By GokiSoft.com|
19:33 29/07/2022|
Java Advanced
[Source Code] Bài toán đa luồng (MultiThreading in java) đọc File trong Java - C2108L
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 java2.bt1105;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author Diep.Tran
*/
public class Thread3 extends Thread{
@Override
public void run() {
SharedData sharedData = SharedData.getInstance();
while(sharedData.isLive()) {
synchronized(sharedData) {
sharedData.notifyAll();
try {
while(sharedData.getCurrentThread() != SharedData.THREAD_3
&& sharedData.isLive()) {
sharedData.wait();
}
if(!sharedData.isLive()) break;
} catch (InterruptedException ex) {
Logger.getLogger(Thread2.class.getName()).log(Level.SEVERE, null, ex);
}
int rand = SharedData.getInstance().getN();
System.out.println("T3 > " + rand * rand);
sharedData.setCurrentThread(SharedData.THREAD_1);
}
}
System.out.println("T3 stop");
}
}
#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 java2.bt1105;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author Diep.Tran
*/
public class Thread2 extends Thread{
@Override
public void run() {
SharedData sharedData = SharedData.getInstance();
while(sharedData.isLive()) {
synchronized(sharedData) {
sharedData.notifyAll();
try {
while(sharedData.getCurrentThread() != SharedData.THREAD_2
&& sharedData.isLive()) {
sharedData.wait();
}
if(!sharedData.isLive()) break;
} catch (InterruptedException ex) {
Logger.getLogger(Thread2.class.getName()).log(Level.SEVERE, null, ex);
}
int rand = SharedData.getInstance().getN();
System.out.println("T2 > ");
for (int i = 1; i <= rand; i++) {
if(rand % i == 0) {
System.out.print(i + " ");
}
}
System.out.println("");
sharedData.setCurrentThread(SharedData.THREAD_1);
}
}
System.out.println("T2 stop");
}
}
#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 java2.bt1105;
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{
@Override
public void run() {
try {
Thread.sleep(100);
} catch (InterruptedException ex) {
Logger.getLogger(Thread1.class.getName()).log(Level.SEVERE, null, ex);
}
FileReader reader = null;
BufferedReader bReader = null;
SharedData sharedData = SharedData.getInstance();
try {
reader = new FileReader("student.txt");
bReader = new BufferedReader(reader);
bReader.readLine();
String line;
while((line = bReader.readLine()) != null) {
synchronized(sharedData) {
//Dong bo du lieu
int rad = Integer.parseInt(line);
SharedData.getInstance().setN(rad);
System.out.println("T1 > " + rad);
if(rad % 2 == 0) {
sharedData.setCurrentThread(SharedData.THREAD_2);
} else {
sharedData.setCurrentThread(SharedData.THREAD_3);
}
sharedData.notifyAll();
try {
while(sharedData.getCurrentThread() != SharedData.THREAD_1) {
sharedData.wait();
}
} catch (InterruptedException ex) {
Logger.getLogger(Thread1.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
} catch (FileNotFoundException ex) {
Logger.getLogger(Thread3.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(Thread3.class.getName()).log(Level.SEVERE, null, ex);
} finally {
if(bReader != null) {
try {
bReader.close();
} catch (IOException ex) {
Logger.getLogger(Thread3.class.getName()).log(Level.SEVERE, null, ex);
}
}
if(reader != null) {
try {
reader.close();
} catch (IOException ex) {
Logger.getLogger(Thread3.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
SharedData.getInstance().setLive(false);
synchronized(sharedData) {
sharedData.notifyAll();
}
System.out.println("T1 stop");
}
}
#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.bt1105;
/**
*
* @author Diep.Tran
*/
public class SharedData {
public static final int THREAD_1 = 1;
public static final int THREAD_2 = 2;
public static final int THREAD_3 = 3;
int n;
boolean live = true;
int currentThread = THREAD_1;
private static SharedData instance = null;
private SharedData() {
}
public synchronized static SharedData getInstance() {
if(instance == null) {
instance = new SharedData();
}
return instance;
}
public int getN() {
return n;
}
public void setN(int n) {
this.n = n;
}
public boolean isLive() {
return live;
}
public void setLive(boolean live) {
this.live = live;
}
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 java2.bt1105;
import java.io.BufferedWriter;
import java.io.FileWriter;
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) {
//createFile();
Thread1 t1 = new Thread1();
Thread2 t2 = new Thread2();
Thread3 t3 = new Thread3();
t1.start();
t2.start();
t3.start();
}
static void createFile() {
FileWriter writer = null;
BufferedWriter bWriter = null;
Random random = new Random();
int N = random.nextInt(90) + 10;
try {
writer = new FileWriter("student.txt");
bWriter = new BufferedWriter(writer);
bWriter.write(N + "\n");
for (int i = 0; i < N; i++) {
int rand = random.nextInt(500) + 1;
bWriter.write(rand + "\n");
}
} catch (IOException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
} finally {
if(bWriter != null) {
try {
bWriter.close();
} catch (IOException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
}
if(writer != null) {
try {
writer.close();
} catch (IOException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
}
Tags:
Phản hồi từ học viên
5
(Dựa trên đánh giá ngày hôm nay)