By GokiSoft.com| 15:17 30/03/2020|
Java Advanced

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

Hướng dẫn chữa bài tập

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



/*
 * 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 java2.lession5.bt01;

/**
 *
 * @author Diep.Tran
 */
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 java2.lession5.bt01;

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

/**
 *
 * @author Diep.Tran
 */
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 java2.lession5.bt01;

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

/**
 *
 * @author Diep.Tran
 */
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");
    }
}



/*
 * 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 java2.lession5.bt01;

/**
 *
 * @author Diep.Tran
 */
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();
    }
}


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

5

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