By GokiSoft.com| 19:17 25/05/2020|
Java Advanced

[Share Code] Sử dụng MultiThreading sinh ký tự a-zA-Z trong java - Lập trình Java - Thread trong Java

[Share Code] Sử dụng MultiThreading sinh ký tự a-zA-Z trong java - Lập trình Java - Thread trong Java



#Main.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 lession6;

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


#SharedData.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 lession6;

/**
 *
 * @author Diep.Tran
 */
public class SharedData {
    char c;
    int totalTime = 0;
    
    private static SharedData instance = null;
    
    private SharedData() {
    }
    
    public synchronized static SharedData getInstance() {
        if(instance == null) {
            instance = new SharedData();
        }
        return instance;
    }

    public void addTime(int more) {
        totalTime += more;
    }
    
    public boolean checkAvaiable() {
        return totalTime <= 20000;
    }
    
    public char getC() {
        return c;
    }

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


#Thread1.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 lession6;

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

/**
 *
 * @author Diep.Tran
 */
public class Thread1 extends Thread{

    @Override
    public void run() {
        try {
            Thread.sleep(100);
        } catch (InterruptedException ex) {
            Logger.getLogger(Thread2.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        int min = (int) 'a';
        int max = (int) 'z';
        
        int delta = max - min;
        
        Random random = new Random();
        
        while(SharedData.getInstance().checkAvaiable()) {
            synchronized(SharedData.getInstance()) {
                int rad = random.nextInt(delta + 1) + min;
                char c = (char) rad;

                SharedData.getInstance().setC(c);

                System.out.println("Thread 1 >> " + c);
                
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException ex) {
                    Logger.getLogger(Thread1.class.getName()).log(Level.SEVERE, null, ex);
                }
                SharedData.getInstance().addTime(2000);
                
                //thiet lap trang thai cho + danh thuc thread2
                SharedData.getInstance().notifyAll();
                try {
                    SharedData.getInstance().wait();
                } catch (InterruptedException ex) {
                    Logger.getLogger(Thread1.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
        
        System.out.println("Stop Thread1");
        synchronized(SharedData.getInstance()) {
            SharedData.getInstance().notifyAll();
        }
    }
    
}


#Thread2.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 lession6;

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

/**
 *
 * @author Diep.Tran
 */
public class Thread2 extends Thread{

    @Override
    public void run() {
        while(SharedData.getInstance().checkAvaiable()) {
            synchronized(SharedData.getInstance()) {
                SharedData.getInstance().notifyAll();
                try {
                    SharedData.getInstance().wait();
                } catch (InterruptedException ex) {
                    Logger.getLogger(Thread2.class.getName()).log(Level.SEVERE, null, ex);
                }
                
                char c = SharedData.getInstance().getC();
                int code = (int) c - 32;
                c = (char) code;

                System.out.println("Thread 2 >> " + c);

                try {
                    Thread.sleep(1000);
                } catch (InterruptedException ex) {
                    Logger.getLogger(Thread2.class.getName()).log(Level.SEVERE, null, ex);
                }
                SharedData.getInstance().addTime(1000);
            }
        }
        
        System.out.println("Stop Thread2");
        
        synchronized(SharedData.getInstance()) {
            SharedData.getInstance().notifyAll();
        }
    }
    
}


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

5

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