By GokiSoft.com| 15:29 26/07/2023|
Java Advanced

Sử dụng MultiThreading sinh ký tự a-zA-Z trong java

Viết chương trình thực hiện yêu cầu sau

- Thread 1 thực hiện sinh ngẫu nhiên các ký tự a-z, cứ 2s thì sinh 1 lần

- Thread 2 thực hiện biến các ký tự thường được sinh ra từ Thread 1 thành ký tự hoa, cứ sau 1s thì thực hiện 1 lần.

Yêu cầu đồng bộ 2 Thread (wait, notify)

- Nếu tổng thời gian xử lý của 2 Thread là 20s thì dựng 2 thread lại.

Phản hồi từ học viên

5

(Dựa trên đánh giá ngày hôm nay)

Ngô Quang Huy [C1907L]
Ngô Quang Huy

2020-05-23 11:14:32



/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package May22Thread;


/**
 *
 * @author student
 */
public class Main {
    public static void main(String[] args) {
        ShareData shareData = new ShareData();
        Thread1 t1 = new Thread1(shareData);
        t1.start();
        Thread2 t2 = new Thread2(shareData);
        t2.start();
    }
}



/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package May22Thread;

/**
 *
 * @author student
 */
public class ShareData {
    long begin;
    char alp;

    public char getAlp() {
        return alp;
    }

    public void setAlp(char alp) {
        this.alp = alp;
    }

    public long getBegin() {
        return begin;
    }

    public void setBegin(long begin) {
        this.begin = begin;
    }

    public ShareData() {
    }

    
}



/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package May22Thread;

import java.util.Random;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author student
 */
public class Thread1 extends Thread{
    ShareData shareData;

    public Thread1(ShareData shareData) {
        this.shareData = shareData;
    }
    
    @Override
    public void run() {
        Random random = new Random();
        shareData.setBegin((long) System.currentTimeMillis());
        for(;;){
            synchronized(shareData){
                char c = (char) (random.nextInt(26)+97);
                System.out.println(c);
                shareData.setAlp(c);
                if((((long) System.currentTimeMillis()-shareData.begin)/1000)>20){
                    System.exit(0);
                }
                try {
                    sleep(2000);
                } catch (InterruptedException ex) {
                    Logger.getLogger(Thread1.class.getName()).log(Level.SEVERE, null, ex);
                }
                try {
                    shareData.notifyAll();
                    shareData.wait();
                } catch (InterruptedException ex) {
                    Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
    }
    
}



/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package May22Thread;

import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author student
 */
public class Thread2 extends Thread{
    ShareData shareData;

    public Thread2(ShareData shareData) {
        this.shareData = shareData;
    }
    
    @Override
    public void run() {
        for(;;){
            synchronized(shareData){
                char c = shareData.getAlp();
                System.out.println(Character.toUpperCase(c));
                if((((long) System.currentTimeMillis()-shareData.begin)/1000)>20){
                    System.exit(0);
                }
                try {
                    sleep(1000);
                } catch (InterruptedException ex) {
                    Logger.getLogger(Thread2.class.getName()).log(Level.SEVERE, null, ex);
                }
                try {
                    shareData.notifyAll();
                    shareData.wait();
                } catch (InterruptedException ex) {
                    Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
    }
    
    
    
}



Nguyễn Hoàng Anh [C1907L]
Nguyễn Hoàng Anh

2020-05-23 09:15:12



/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package MultiThreadingGenerate_a_Z;

import java.util.Random;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Redmibook 14
 */
public class Thread1 extends Thread {

    SharedData sharedData;

    public Thread1(SharedData sharedData) {
        this.sharedData = sharedData;
    }

    @Override
    public void run() {
        Random rd = new Random();

        for (;;) {
            synchronized (sharedData) {
                char c = (char) (rd.nextInt(26) + 97);
                sharedData.setC(c);
                System.out.println(sharedData.getC());

                try {
                    if (checkTime()) {
                        break;
                    }
                    sleep(2000);

                } catch (InterruptedException ex) {
                    Logger.getLogger(Thread1.class.getName()).log(Level.SEVERE, null, ex);
                }

                try {
                    sharedData.notifyAll();
                    sharedData.wait();
                } catch (InterruptedException ex) {
                    Logger.getLogger(Thread1.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
           
        }
    }

    Boolean checkTime() {
        return ((System.currentTimeMillis() - sharedData.begin) / 1000 > 20);

    }
}



/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package MultiThreadingGenerate_a_Z;

import java.util.Random;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Redmibook 14
 */
public class Thread2 extends Thread {

    SharedData sharedData;

    public Thread2(SharedData sharedData) {
        this.sharedData = sharedData;
    }

    @Override
    public void run() {
        Random rd = new Random();

        for (;;) {
            synchronized (sharedData) {
                char c = (char) (rd.nextInt(26) + 65);
                sharedData.setC(c);
                System.out.println(sharedData.getC());

                try {
                    if (checkTime()) {
                        break;
                    }
                    sleep(1000);

                } catch (InterruptedException ex) {
                    Logger.getLogger(Thread2.class.getName()).log(Level.SEVERE, null, ex);
                }

                try {
                    sharedData.notifyAll();
                    sharedData.wait();
                } catch (InterruptedException ex) {
                    Logger.getLogger(Thread2.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
           
        }
    }

    Boolean checkTime() {
        return ((System.currentTimeMillis() - sharedData.begin) / 1000 > 20);

    }
}



/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package MultiThreadingGenerate_a_Z;

/**
 *
 * @author Redmibook 14
 */
public class Test {

    public static void main(String[] args) {
        SharedData sharedData = new SharedData();
        sharedData.setBegin(System.currentTimeMillis());
        Thread1 t1 = new Thread1(sharedData);
        Thread2 t2 = new Thread2(sharedData);
        t1.start();
        t2.start();
    }
}



/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package MultiThreadingGenerate_a_Z;

/**
 *
 * @author Redmibook 14
 */
public class SharedData {
    public char c;
    public long begin;

    public char getC() {
        return c;
    }

    public long getBegin() {
        return begin;
    }

    public void setC(char c) {
        this.c = c;
    }

    public void setBegin(long begin) {
        this.begin = begin;
    }

  
    
}



trung [C1907L]
trung

2020-05-22 14:04:19



/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package daerht;

/**
 *
 * @author student
 */
public class charGenerator {
    public static void main(String[] args) {
        sharedData data = new sharedData();
        data.setStartTime(System.currentTimeMillis());
        Thread1 t1 = new Thread1(data);
        Thread t2 = new Thread2(data);
        t1.start();
        t2.start();
    }
}



/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package daerht;

/**
 *
 * @author student
 */
public class sharedData {
    char x;
    long startTime;

    public sharedData() {
    }

    public char getX() {
        return x;
    }

    public void setX(char x) {
        this.x = x;
    }

    public long getStartTime() {
        return startTime;
    }

    public void setStartTime(long startTime) {
        this.startTime = startTime;
    }
    
}



/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package daerht;

import java.util.Random;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author student
 */
public class Thread1 extends Thread {

    sharedData data;

    public Thread1(sharedData data) {
        this.data = data;
    }

    @Override
    public void run() {
        Random rd = new Random();
        while (true) {
            synchronized (data) {
                char x = (char) (rd.nextInt(26) + 97);
                System.out.println(x);
                this.data.setX(x);
                try {
                    if (checkStop()) {
                        break;
                    }
                    sleep(0x7d0);
                } catch (InterruptedException ex) {
                    Logger.getLogger(Thread1.class.getName()).log(Level.SEVERE, null, ex);
                }

                try {
                    data.notifyAll();
                    data.wait();
                } catch (InterruptedException ex) {
                    Logger.getLogger(Thread1.class.getName()).log(Level.SEVERE, null, ex);
                }
            }

        }
        synchronized (data) {
            data.notifyAll();
        }
    }

    boolean checkStop() {
        return ((System.currentTimeMillis() - this.data.startTime) > 20000);
    }
}



/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package daerht;

import static java.lang.Thread.sleep;
import java.util.Random;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author student
 */
public class Thread2 extends Thread {

    sharedData data;

    public Thread2(sharedData data) {
        this.data = data;
    }

    @Override
    public void run() {
        Random rd = new Random();
        while (true) {
            synchronized (data) {
                System.out.println(Character.toUpperCase(this.data.getX()));
                try {
                    if (checkStop()) {
                        break;
                    }
                    sleep(0x3e8);
                } catch (InterruptedException ex) {
                    Logger.getLogger(Thread1.class.getName()).log(Level.SEVERE, null, ex);
                }

                try {
                    data.notifyAll();
                    data.wait();
                } catch (InterruptedException ex) {
                    Logger.getLogger(Thread1.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
        synchronized (data) {
            data.notifyAll();
        }
    }

    boolean checkStop() {
        return ((System.currentTimeMillis() - this.data.startTime) > 20000);
    }
}



Phí Văn Long [T1907A]
Phí Văn Long

2020-04-18 05:53:24



/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package multiThreading;

/**
 *
 * @author Nong
 */
public class Main {
       public static void main(String[] args) {
        ShareData data = new ShareData();

        Thread1 thread1 = new Thread1(data);
        Thread2 thread2 = new Thread2(data);

        thread1.start();
        thread2.start();
    }
}



/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package multiThreading;

import java.util.Random;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Nong
 */
public class Thread1 extends Thread {
    ShareData data;

    public Thread1(ShareData data) {
        this.data = data;
    }

    Random random = new Random();

    @Override
    public void run() {

        try {
            Thread.sleep(200);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        while (data.checkTime()) {
            synchronized (data) {

                try {
                    
                    int minrad = (int) 'a';
                    int maxrad = (int) 'z';
                    int b = maxrad - minrad;
                    
                    int rad = random.nextInt(b) + minrad;
                    char c = (char) rad;
                    System.out.println("t1: " + c);
                    data.setRad(rad);
                    try {
                        Thread.sleep(500);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    
                    data.addtime(2000);
                    
                    
                    data.notifyAll();
                    data.wait();
                } catch (InterruptedException ex) {
                    Logger.getLogger(Thread1.class.getName()).log(Level.SEVERE, null, ex);
                }
            }

        }
    }
}



/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package multiThreading;

/**
 *
 * @author Nong
 */
public class Thread2 extends Thread {
    ShareData data;

    public Thread2(ShareData data) {
        this.data = data;
    }

    @Override
    public void run() {
        while (data.checkTime()) {
            synchronized (data){
                data.notifyAll();
                try {
                    data.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                int rad = data.getRad();
                char b = (char) (rad - 32);
                System.out.println("t2:" + b);
                try {
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                data.addtime(500);
            }

        }
        synchronized (data){
           data.notifyAll();
        }
    }
}



/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package multiThreading;

/**
 *
 * @author Nong
 */
public class ShareData {
    int rad;
    int time = 0;

    public ShareData() {
    }

    public int getRad() {
        return rad;
    }

    public void setRad(int rad) {
        this.rad = rad;
    }

    public int getTime() {
        return time;
    }

    public void setTime(int time) {
        this.time = time;
    }

    public synchronized void addtime(int a) {
        time += a;
    }

    public synchronized boolean checkTime() {
        if (time >= 20000) {
            return false;
        }
        return true;
    }
}



NguyenHuuThanh [T1907A]
NguyenHuuThanh

2020-04-01 09:58:28



/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package multithreadbt5;

/**
 *
 * @author abc
 */
public class Multithreadbt5 {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        SharedData sharedData = new SharedData();
        Thread1 thread1 = new Thread1(sharedData);
        Thread2 thread2 = new Thread2(sharedData);
        thread1.start();
        thread2.start();
    }
    
}
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package multithreadbt5;

/**
 *
 * @author abc
 */
public class SharedData {
    char c;
    int total;
    
    public SharedData()
    {
        total = 0;
    }

    public char getC() {
        return c;
    }

    public void setC(char c) {
        this.c = c;
    }

    public int getTotal() {
        return total;
    }

    public void setTotal(int total) {
        this.total = total;
    }
    
    public synchronized void plus(int value)
    {
        total += value;
    }
    
    public synchronized boolean checkAvailable()
    {
        return total < 20000;
    }

    
    
    
}
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package multithreadbt5;

import java.util.Random;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author abc
 */
public class Thread1 extends Thread {
    SharedData sharedData;

    public Thread1(SharedData sharedData) {
        this.sharedData = sharedData;
    }
    
    
    
    @Override
    public void run()
    {
        try {
            Thread.sleep(200);
        } catch (InterruptedException ex) {
            Logger.getLogger(Thread1.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        
        
        
        
        int mincode = (int) 'a';
        int maxcode = (int) 'z';
        int limit = maxcode - mincode;
        
        while(sharedData.checkAvailable())
        {
            synchronized(sharedData)
            {
              
                
                
            Random random = new Random();
            
            int rad = random.nextInt(limit) + mincode;
            
            char c = (char) rad;
            
            System.out.println(" char rad :" + c);
            sharedData.setC(c);
            
            
            try {
                Thread.sleep(2000);
            } catch (InterruptedException ex) {
                Logger.getLogger(Thread1.class.getName()).log(Level.SEVERE, null, ex);
            }
            
            sharedData.plus(2000);
                
            
            sharedData.notifyAll();
            
                try {
                    sharedData.wait();
                } catch (InterruptedException ex) {
                    Logger.getLogger(Thread1.class.getName()).log(Level.SEVERE, null, ex);
                }
                
            }
        }
        
        System.out.println("Thread1 : stop");
        synchronized(sharedData)
        {
            sharedData.notifyAll();
        }
   }
    
}
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package multithreadbt5;

import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author abc
 */
public class Thread2 extends Thread {
    SharedData sharedData;

    public Thread2(SharedData sharedData) {
        this.sharedData = sharedData;
    }
    
    
    @Override
    public void run()
    {
        while(sharedData.checkAvailable())
        {
            synchronized(sharedData)
            {
                sharedData.notifyAll();
                
                try {
                    sharedData.wait();
                } catch (InterruptedException ex) {
                    Logger.getLogger(Thread2.class.getName()).log(Level.SEVERE, null, ex);
                }
                
            char c = sharedData.getC();
            
            int code = (int) c - 32 ;
            c = (char) code ;
            System.out.println("T2 :" + c);
            
            try {
                Thread.sleep(1000);
            } catch (InterruptedException ex) {
                Logger.getLogger(Thread2.class.getName()).log(Level.SEVERE, null, ex);
            }
            
            sharedData.plus(1000);
            }
         }
        System.out.println("Thread2 : stop");
        synchronized(sharedData)
        {
            sharedData.notifyAll();
        }
   }
    
    
}




Đỗ Văn Huấn [T1907A]
Đỗ Văn Huấn

2020-03-31 14:24:35



package java2_Advanced.BaiTapNgay30_3_2020.KyTuTuAZ;

public class Data {
    int rad;
    int time = 0;

    public Data() {
    }

    public int getRad() {
        return rad;
    }

    public void setRad(int rad) {
        this.rad = rad;
    }

    public int getTime() {
        return time;
    }

    public void setTime(int time) {
        this.time = time;
    }

    public synchronized void addtime(int a) {
        time += a;
    }

    public synchronized boolean chekTime() {
        if (time >= 20000) {
            return false;
        }
        return true;
    }
}



package java2_Advanced.BaiTapNgay30_3_2020.KyTuTuAZ;

import java.util.Random;

public class Thread1 extends Thread {
    Data data;

    public Thread1(Data data) {
        this.data = data;
    }

    Random random = new Random();

    @Override
    public void run() {

        try {
            Thread.sleep(200);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        while (data.chekTime()) {
            synchronized (data) {

                int minrad = (int) 'a';
                int maxrad = (int) 'z';
                int b = maxrad - minrad;

                int rad = random.nextInt(b) + minrad;
                char c = (char) rad;                            // ép sang kí tự
                System.out.println("rad: " + c);
                data.setRad(rad);
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                data.addtime(2000);                            // thêm thời gian vào addTime


                data.notifyAll();
                try {
                    data.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

        }
    }
}



package java2_Advanced.BaiTapNgay30_3_2020.KyTuTuAZ;

public class Thread2 extends Thread {
    Data data;

    public Thread2(Data data) {
        this.data = data;
    }

    @Override
    public void run() {
        while (data.chekTime()) {
            synchronized (data){
                data.notifyAll();
                try {
                    data.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                int rad = data.getRad();
                char b = (char) (rad - 32);
                System.out.println("t2:" + b);
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                data.addtime(1000);
            }

        }
        synchronized (data){
           data.notifyAll();
        }
    }
}



package java2_Advanced.BaiTapNgay30_3_2020.KyTuTuAZ;

public class Main {
    public static void main(String[] args) {
        Data data = new Data();

        Thread1 thread1 = new Thread1(data);
        Thread2 thread2 = new Thread2(data);

        thread1.start();
        thread2.start();
    }
}



thienphu [T1907A]
thienphu

2020-03-31 13:50:38



/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package MutiThreading;

/**
 *
 * @author Thien Phu
 */
public class Kitu {

    int kitu;
    int time;

    public Kitu() {
        time = 0;
    }

    public void setKitu(char kitu) {
        this.kitu = kitu;
    }

    public int getKitu() {
        return kitu;
    }

    public void setTime(int time) {
        this.time = time;
    }

    public int getTime() {
        return time;
    }

    public void TongTime(int value) {
        time += value;
    }

    public synchronized boolean checkTime() {
        if (time >= 20000) {
            return false;
        }
        return true;
    }
}



/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package MutiThreading;

import java.util.Random;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Thien Phu
 */
public class ThreadRandom extends Thread {

    Kitu kitu;

    public ThreadRandom(Kitu kitu) {
        this.kitu = kitu;
    }

    @Override
    public void run() {
        try {
            Thread.sleep(1000);
        } catch (InterruptedException ex) {
            Logger.getLogger(ThreadRandom.class.getName()).log(Level.SEVERE, null, ex);
        }
        synchronized (kitu) {
            while (kitu.checkTime()) {
                Random r = new Random();
                int ramdom = r.nextInt(25) + 97;
                char number = (char) ramdom;
                System.out.println("chu thuong: " + number);
                kitu.setKitu(number);
                kitu.notifyAll();
                try {
                    kitu.wait();
                } catch (InterruptedException ex) {
                    Logger.getLogger(ThreadRandom.class.getName()).log(Level.SEVERE, null, ex);
                }
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException ex) {
                    Logger.getLogger(ThreadRandom.class.getName()).log(Level.SEVERE, null, ex);
                }
                kitu.TongTime(2000);
            }
            synchronized (kitu) {
                System.out.println("Stop t1");
                kitu.notifyAll();
                stop();
            }
        }
    }

}



/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package MutiThreading;

import java.util.Random;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Thien Phu
 */
public class Thread2 extends Thread {

    Kitu kitu;

    public Thread2(Kitu kitu) {
        this.kitu = kitu;
    }

    @Override
    public void run() {
        synchronized (kitu) {
            while (kitu.checkTime()) {
                kitu.notifyAll();
                try {
                    kitu.wait();
                } catch (InterruptedException ex) {
                    Logger.getLogger(Thread2.class.getName()).log(Level.SEVERE, null, ex);
                }
                if (kitu.checkTime()) {

                    int number = kitu.getKitu() - 32;
                    char inhoa = (char) (number);
                    System.out.println("In hoa: " + inhoa);
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException ex) {
                        Logger.getLogger(Thread2.class.getName()).log(Level.SEVERE, null, ex);
                    }
                    kitu.TongTime(1000);
                }
            }
            synchronized (kitu) {
                System.out.println("Stop t2");
                stop();

            }
        }
    }

}



/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package MutiThreading;

/**
 *
 * @author Thien Phu
 */
public class Main {

    public static void main(String[] args) {
        Kitu kitu = new Kitu();
        ThreadRandom t1 = new ThreadRandom(kitu);
        Thread2 t2 = new Thread2(kitu);
        t2.start();
        t1.start();
    }
}



lê văn phương [T1907A]
lê văn phương

2020-03-31 09:52:11



/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package MultiThreading;

/**
 *
 * @author HOME
 */
public class Main {
     public static void main(String[] args) {
        SharedData sharedData = new SharedData();
        Thread1 t1 = new Thread1(sharedData);
        Thread2 t2 = new Thread2(sharedData);
        
        t1.start();
        t2.start();
    }
}



/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package MultiThreading;

/**
 *
 * @author HOME
 */
public class SharedData {
    char radC;
    int totalTime = 0;

    public SharedData() {
    }

    public char getRadC() {
        return radC;
    }

    public void setRadC(char radC) {
        this.radC = radC;
    }

    public int getTotalTime() {
        return totalTime;
    }

    public void setTotalTime(int totalTime) {
        this.totalTime = totalTime;
    }
    
    public synchronized void addTime(int time) {
        totalTime += time;
    }
    
    public synchronized boolean checkAvaiable() {
        if(totalTime >= 20000) {
            return false;
        }
        return true;
    }
}



/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package MultiThreading;

import java.util.Random;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author HOME
 */
public class Thread1 extends Thread{
     SharedData sharedData;

    public Thread1(SharedData sharedData) {
        this.sharedData = sharedData;
    }
    
     @Override
    public void run() {
        try {
            Thread.sleep(300);
        } catch (InterruptedException ex) {
            Logger.getLogger(Thread1.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        int minCode = (int) 'a';
        int maxCode = (int) 'z';
        int limit = maxCode - minCode;
        
        Random random = new Random();
        
        while (sharedData.checkAvaiable()) {            
            synchronized(sharedData) {
                int rad = random.nextInt(limit) + minCode;
                char c = (char) rad;
                System.out.println(c);

                sharedData.setRadC(c);
                sharedData.notifyAll();
                try {
                    sharedData.wait();
                } catch (InterruptedException ex) {
                    Logger.getLogger(Thread1.class.getName()).log(Level.SEVERE, null, ex);
                }
                
                try {
                    //delay - OK - chuan.
                    Thread.sleep(2000);
                } catch (InterruptedException ex) {
                    Logger.getLogger(Thread1.class.getName()).log(Level.SEVERE, null, ex);
                }
                //thay doi tgian len he thong
                sharedData.addTime(2000);
            }
        }
        
        System.out.println("T1 Stop");
        synchronized(sharedData) {
            sharedData.notifyAll();
        }
    }
    
}



/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package MultiThreading;

import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author HOME
 */
public class Thread2 extends Thread{
    SharedData sharedData;

    public Thread2(SharedData sharedData) {
        this.sharedData = sharedData;
    }

    @Override
    public void run() {
        while (sharedData.checkAvaiable()) {
            synchronized(sharedData) {
                sharedData.notifyAll();
                try {
                    sharedData.wait();
                } catch (InterruptedException ex) {
                    Logger.getLogger(Thread2.class.getName()).log(Level.SEVERE, null, ex);
                }
                
                char c = sharedData.getRadC();
                //chuyen ky c => ky tu C (tu ky thuong thanh ky tu hoa)
                int code = (int) c - 32;
                c = (char) code;
                System.out.println("T2: " + c);

                try {
                    //delay thoi gian => 1s
                    Thread.sleep(1000);
                } catch (InterruptedException ex) {
                    Logger.getLogger(Thread2.class.getName()).log(Level.SEVERE, null, ex);
                }

                //add them vao SharedData
                sharedData.addTime(1000);
            }
        }
        
        System.out.println("T2 Stop");
    }
}