By GokiSoft.com|
20:01 22/07/2021|
Java Advanced
[Share Code] Bài toán đa luồng (MultiThreading in java) đọc File trong Java - C2005L
Bài toán đa luồng (MultiThreading in java) đọc File trong Java
#Thread3.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 lesson3;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author Diep.Tran
*/
public class Thread3 extends Thread{
SharedData sharedData;
public Thread3(SharedData sharedData) {
this.sharedData = sharedData;
}
@Override
public void run() {
while(!sharedData.isStop()) {
synchronized(sharedData) {
sharedData.notifyAll();
try {
// while(sharedData.getCurrentThread() != SharedData.THREAD3) {
// sharedData.wait();
//
// if(sharedData.isStop()) {
// sharedData.notifyAll();
// System.out.println("T3 stop");
// return;
// }
// }
while(!sharedData.isStop() && sharedData.getCurrentThread() != SharedData.THREAD3) {
sharedData.wait();
}
if(sharedData.isStop()) break;
} catch (InterruptedException ex) {
Logger.getLogger(Thread3.class.getName()).log(Level.SEVERE, null, ex);
}
int number = sharedData.getNumber();
System.out.println("BP: " + (number * number));
sharedData.setCurrentThread(SharedData.THREAD1);
}
}
System.out.println("T3 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 lesson3;
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.isStop()) {
synchronized(sharedData) {
sharedData.notifyAll();
try {
// while(sharedData.getCurrentThread() != SharedData.THREAD2) {
// sharedData.wait();
// if(sharedData.isStop()) {
// sharedData.notifyAll();
// System.out.println("T2 stop");
// return;
// }
// }
while(!sharedData.isStop() && sharedData.getCurrentThread() != SharedData.THREAD2) {
sharedData.wait();
}
if(sharedData.isStop()) break;
} catch (InterruptedException ex) {
Logger.getLogger(Thread2.class.getName()).log(Level.SEVERE, null, ex);
}
int number = sharedData.getNumber();
System.out.printf("\nT2 (" + number + "): ");
for (int i = 1; i <= number; i++) {
if(number % i == 0) {
System.out.printf(i + ", ");
}
}
System.out.println("");
sharedData.setCurrentThread(SharedData.THREAD1);
}
}
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 lesson3;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
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() {
FileReader reader = null;
BufferedReader bReader = null;
try {
reader = new FileReader("number.txt");
bReader = new BufferedReader(reader);
//Loai bo so N trong file number.txt
bReader.readLine();
while(!sharedData.isStop()) {
synchronized(sharedData) {
String s = bReader.readLine();
if(s == null || s.isEmpty()) {
sharedData.setStop(true);
break;
}
int number = Integer.parseInt(s);
System.out.println("Number: " + number);
sharedData.setNumber(number);
sharedData.notifyAll();
try {
while(sharedData.getCurrentThread() != SharedData.THREAD1) {
sharedData.wait();
}
} catch (InterruptedException ex) {
Logger.getLogger(Thread1.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
} catch (FileNotFoundException ex) {
Logger.getLogger(Thread1.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(Thread1.class.getName()).log(Level.SEVERE, null, ex);
} finally {
if(reader != null) {
try {
reader.close();
} catch (IOException ex) {
Logger.getLogger(Thread1.class.getName()).log(Level.SEVERE, null, ex);
}
}
if(bReader != null) {
try {
bReader.close();
} catch (IOException 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 lesson3;
/**
*
* @author Diep.Tran
*/
public class SharedData {
public static final int THREAD1 = 1;
public static final int THREAD2 = 2;
public static final int THREAD3 = 3;
int number;
boolean stop;
int currentThread = THREAD1;
public SharedData() {
stop = false;
}
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
if(number % 2 == 0) {
currentThread = THREAD2;
} else {
currentThread = THREAD3;
}
}
public boolean isStop() {
return stop;
}
public void setStop(boolean stop) {
this.stop = stop;
}
public int getCurrentThread() {
return currentThread;
}
public void setCurrentThread(int currentThread) {
this.currentThread = currentThread;
}
}
#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 lesson3;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Random;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author Diep.Tran
*/
public class Main {
public static void main(String[] args) {
// randomNumbers();
SharedData sharedData = new SharedData();
Thread1 t1 = new Thread1(sharedData);
Thread3 t3 = new Thread3(sharedData);
Thread2 t2 = new Thread2(sharedData);
t1.start();
t2.start();
t3.start();
}
static void randomNumbers() {
Random random = new Random();
FileOutputStream fos = null;
try {
fos = new FileOutputStream("number.txt");
int n = random.nextInt(90) + 10;
byte[] b = String.valueOf(n + "\n").getBytes();
fos.write(b);
for (int i = 0; i < n; i++) {
int v = random.nextInt(500) + 1;
b = String.valueOf(v + "\n").getBytes();
fos.write(b);
}
} catch (FileNotFoundException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
} finally {
try {
fos.close();
} catch (IOException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
Phản hồi từ học viên
5
(Dựa trên đánh giá ngày hôm nay)