By GokiSoft.com| 21:02 29/03/2023|
Java Advanced

[Source Code] Luyện tập 60 phút Java - C2206L BT3190

Luyện tập 60 phút 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 com.gokisoft.bt3189;

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

#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 com.gokisoft.bt3189;

/**
 *
 * @author teacher
 */
public class SharedData {
    int index;

    public int getIndex() {
        return index;
    }

    public void setIndex(int index) {
        this.index = index;
    }
}

#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 com.gokisoft.bt3189;

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

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

    SharedData sharedData;
    String[] weeks;

    public Thread1(SharedData sharedData) {
        this.sharedData = sharedData;
        weeks = new String[]{"Thu Hai", "Thu Ba", "Thu Tu", "Thu Nam", "Thu Sau", "Thu Bay", "Chu Nhat"};
    }

    @Override
    public void run() {
        Random random = new Random();
        int length = weeks.length - 1;

        synchronized (sharedData) {
            for (;;) {
                int rad = random.nextInt(length);
                System.out.println("T1 > " + weeks[rad]);
                sharedData.setIndex(rad);
                
                sharedData.notifyAll();
                try {
                    sharedData.wait();
                } catch (InterruptedException ex) {
                    Logger.getLogger(Thread1.class.getName()).log(Level.SEVERE, null, ex);
                }
                
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException ex) {
                    Logger.getLogger(Thread1.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
        
//        System.out.println("T1 stop");
//        synchronized(sharedData) {
//            sharedData.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 com.gokisoft.bt3189;

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

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

    SharedData sharedData;
    String[] weeks;

    public Thread2(SharedData sharedData) {
        this.sharedData = sharedData;
        weeks = new String[]{"Monday", "Tuesday", "Wednesday", "Thurday", "Friday", "Saturday", "Sunday"};
    }

    @Override
    public void run() {
        synchronized (sharedData) {
            for (;;) {
                sharedData.notifyAll();
                try {
                    sharedData.wait();
                } catch (InterruptedException ex) {
                    Logger.getLogger(Thread2.class.getName()).log(Level.SEVERE, null, ex);
                }
                
                int rad = sharedData.getIndex();
                System.out.println("T2 > " + weeks[rad]);
            }
        }
        
        
//        System.out.println("T2 stop");
//        synchronized(sharedData) {
//            sharedData.notifyAll();
//        }
        
    }
}
Tags:

Liên kết rút gọn:

https://gokisoft.com/3190

Bình luận