By GokiSoft.com|
20:03 21/08/2021|
Java Advanced
[Share Code] Thư viện đồng bộ đa luồng - bài toán hiển thị tên và địa chỉ sinh viên - Lập trình Java nâng cao - C2010L
Bài toán:
T1: Sinh ngẫu nhiên các số từ 0 -> 100 (rad)
T2: Nếu rad chẵn -> In ra bình phương số rad từ T1
T3: Nếu rad lẻ -> In ra tất cả các ước số chung của rad.
Yêu cầu:
T1..rad.. ..rad.. ..rad..
T2 ..rad(even)..rad*rad.. ..rad(even)..rad*rad..
T3. ..rad(odd)..in uoc sc(rad)..
================================================================================================
T1..rad..T2|T3..notifyAll..loop:wait ..loop:wait.. ..rad..
T2 ..notifyAll..loop:wait
T3 ..notifyAll..rad..T1..sync..notifyAll..loop:wait
N Thread -> Bai toan tong quat.
#ThreadTwo.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.lesson05.bt1103;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author Diep.Tran
*/
public class ThreadTwo extends SyncThread {
public ThreadTwo() {
super(SharedData.THREAD_2);
}
@Override
public void onRunning() {
while (sharedData.isLive()) {
synchronized (sharedData) {
if(isStop()) {
break;
}
String str = sharedData.getText();
System.out.println("T2 -> " + str);
sharedData.setCurrentThread(SharedData.THREAD_1);
}
}
}
}
#ThreadOne.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.lesson05.bt1103;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author Diep.Tran
*/
public class ThreadOne extends SyncThread{
public ThreadOne() {
super(SharedData.THREAD_1);
}
@Override
public void onRunning() {
while(sharedData.isLive()) {
synchronized(sharedData) {
String str = sharedData.getText();
System.out.println("T1 -> " + str);
sharedData.setCurrentThread(SharedData.THREAD_2);
if(isStop()) {
break;
}
}
}
}
}
#SyncThread.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.lesson05.bt1103;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author Diep.Tran
*/
public abstract class SyncThread extends Thread{
SharedData sharedData;
int id;
public SyncThread(int id) {
sharedData = SharedData.getInstance();
this.id = id;
}
@Override
public void run() {
onRunning();
System.out.println("T" + id + " stop");
synchronized(sharedData) {
sharedData.notifyAll();
}
}
public abstract void onRunning();
/**
* TODO: Bat buoc phai dang trong sync.
* @return
*/
boolean isStop() {
sharedData.notifyAll();
try {
while (sharedData.isLive() && sharedData.getCurrentThread() != id) {
sharedData.wait();
}
if (!sharedData.isLive()) {
return true;
}
} catch (InterruptedException ex) {
Logger.getLogger(SyncThread.class.getName()).log(Level.SEVERE, null, ex);
}
return false;
}
}
#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 java2.lesson05.bt1103;
/**
*
* @author Diep.Tran
*/
public class SharedData {
public static final int THREAD_1 = 1;
public static final int THREAD_2 = 2;
int currentThread = THREAD_1;
boolean live = true;
private static SharedData instance = null;
String[] nameList;
String[] addressList;
int index = 0;
private SharedData() {
nameList = new String[] {"Nguyen Ngoc Lan","Duong Tien Nam","Ngo Anh Quan","Pham Van Duc","Tran Thi Ha"};
addressList = new String[] {"Hải Phòng","Hà Nội","Thái Nguyên","Hà Tĩnh","Quảng Ninh"};
}
public synchronized static SharedData getInstance() {
if(instance == null) {
instance = new SharedData();
}
return instance;
}
public int getCurrentThread() {
return currentThread;
}
public void setCurrentThread(int currentThread) {
this.currentThread = currentThread;
}
public boolean isLive() {
return live;
}
public void setLive(boolean live) {
this.live = live;
}
/**
* length: 5 -> nameList.length, index: 0 -> 4
* index = 0 -> THREAD_1: name (index)
* -> THREAD_2: address (index) -> index++
* ...
* index = 4
* -> THREAD_1: name (index)
* -> THREAD_2: live = false, address (index) -> index++ -> 5 (Ket thuc du lieu)
* @return
*/
public String getText() {
switch(currentThread) {
case THREAD_1:
return nameList[index];
default:
if(index >= (addressList.length-1)) {
live = false;
}
return addressList[index++];
}
}
}
#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 java2.lesson05.bt1103;
/**
*
* @author Diep.Tran
*/
public class Main {
public static void main(String[] args) {
ThreadOne t1 = new ThreadOne();
ThreadTwo t2 = new ThreadTwo();
t1.start();
t2.start();
}
}
Tags:
Phản hồi từ học viên
5
(Dựa trên đánh giá ngày hôm nay)