By GokiSoft.com|
21:11 03/04/2023|
Java Advanced
[Source Code] Sử dụng MultiThreading sinh ký tự a-zA-Z trong java - C2206L
Sử dụng MultiThreading sinh ký tự a-zA-Z trong 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.bt1107;
/**
*
* @author teacher
*/
public class Main {
public static void main(String[] args) {
SharedData sharedData = new SharedData();
// char c = sharedData.getRandomLowerCaseChar();
// char C = sharedData.getUpperCaseChar();
//
// System.out.println(c + " - " + C);
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.bt1107;
import java.util.Random;
/**
*
* @author teacher
*/
public class SharedData {
char radC;
int deltaTime = 0;
public char getRandomLowerCaseChar() {
int min = 'a';
int max = 'z';
int delta = max - min;
Random random = new Random();
int rad = random.nextInt(delta);
int _radC = min + rad;
radC = (char) _radC;
return radC;
}
public char getUpperCaseChar() {
int codea = 'a';
int codeA = 'A';
int delta = codea - codeA;
return (char) (radC - delta);
}
public synchronized void addTime(int delta) {
deltaTime += delta;
}
public boolean isLive() {
return deltaTime <= 20;
}
}
#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.bt1107;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author teacher
*/
public class Thread1 extends Thread {
SharedData sharedData;
public Thread1(SharedData sharedData) {
this.sharedData = sharedData;
}
@Override
public void run() {
try {
Thread.sleep(100);
} catch (InterruptedException ex) {
Logger.getLogger(Thread1.class.getName()).log(Level.SEVERE, null, ex);
}
synchronized (sharedData) {
for (;sharedData.isLive();) {
char c = sharedData.getRandomLowerCaseChar();
System.out.println("T1 > " + c);
sharedData.notifyAll();
try {
sharedData.wait();
} catch (InterruptedException ex) {
Logger.getLogger(Thread1.class.getName()).log(Level.SEVERE, null, ex);
}
try {
Thread.sleep(2000);
sharedData.addTime(2);
} 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.bt1107;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author teacher
*/
public class Thread2 extends Thread {
SharedData sharedData;
public Thread2(SharedData sharedData) {
this.sharedData = sharedData;
}
@Override
public void run() {
synchronized (sharedData) {
for (;sharedData.isLive();) {
sharedData.notifyAll();
try {
sharedData.wait();
} catch (InterruptedException ex) {
Logger.getLogger(Thread2.class.getName()).log(Level.SEVERE, null, ex);
}
char c = sharedData.getUpperCaseChar();
System.out.println("T2 > " + c);
try {
Thread.sleep(1000);
sharedData.addTime(1);
} catch (InterruptedException ex) {
Logger.getLogger(Thread2.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
System.out.println("T2 stop");
synchronized(sharedData) {
sharedData.notifyAll();
}
}
}
Tags:
Phản hồi từ học viên
5
(Dựa trên đánh giá ngày hôm nay)