By GokiSoft.com| 20:03 22/07/2021|
Java Advanced

[Share Code] MultiThread - Sinh số ngẫu nhiên và hiển thị bình phương số ngẫu nhiên & Synchronized trong Java - C2005L

MultiThread - Sinh số ngẫu nhiên và hiển thị bình phương số ngẫu nhiên & Synchronized trong Java

#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 lesson02;

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.isStart()) {
            synchronized(sharedData) {
                sharedData.notifyAll();
                try {
                    sharedData.wait();
                } catch (InterruptedException ex) {
                    Logger.getLogger(Thread2.class.getName()).log(Level.SEVERE, null, ex);
                }
                
                //lay so ngau nhien dc sinh ra tu thread 1 -> lam the nao???
                int rad = sharedData.getRad();
                System.out.println("BP: " + (rad * rad));

//                try {
//                    Thread.sleep(1000);
//                } catch (InterruptedException ex) {
//                    Logger.getLogger(Thread2.class.getName()).log(Level.SEVERE, null, ex);
//                }
            }
        }
        System.out.println("T2 stop");
        synchronized(sharedData) {
            sharedData.notifyAll();
        }
    }
    
}


#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 lesson02;

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() {
        Random random = new Random();
        
        while(sharedData.isStart()) {
            synchronized(sharedData) {
                int rad = random.nextInt(20) + 1;
                sharedData.setRad(rad);
                System.out.println("Rad: " + rad);

//                try {
//                    Thread.sleep(2000);
//                } catch (InterruptedException ex) {
//                    Logger.getLogger(Thread1.class.getName()).log(Level.SEVERE, null, ex);
//                }
                
                sharedData.notifyAll();
                try {
                    sharedData.wait();
                } catch (InterruptedException ex) {
                    Logger.getLogger(Thread1.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
        
        System.out.println("T1 stop");
        synchronized(sharedData) {
            sharedData.notifyAll();
        }
    }
    
}


#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 lesson02;

/**
 *
 * @author Diep.Tran
 */
public class SharedData {
    int rad;
    int count;
    
    public SharedData() {
        count = 0;
    }

    public int getRad() {
        return rad;
    }

    public void setRad(int rad) {
        this.rad = rad;
        count++;
    }
    
    public boolean isStart() {
        return count < 20;
    }
}


#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 lesson02;

/**
 *
 * @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)