该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
import java.util.Random;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class Guess {
public int number = -1 ;
public volatile int number2;
public volatile int number3;
public Run2 r2 ;
public int count=0;
public Lock lock1 = new ReentrantLock();
public Object o = new Object();
public Object o2 = new Object();
public Run3 r3 ;
private static ExecutorService executor;
public static void main(String[] args){
ExecutorService e = Executors.newSingleThreadExecutor();
e.execute(new Guess().new Run1());
e.shutdown();
}
public class Run1 implements Runnable{
@Override
public void run() {
init();
Random r = new Random();
number = r.nextInt(100);
System.out.println("标准数字:" + number);
synchronized (o) {
o.notifyAll();
}
try {
TimeUnit.SECONDS.sleep(2);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
for(;;){
//lock1.lock();
count=0;
if(number==number2){
System.out.println("线程2正确");
break;
}
if(number>number2){
System.out.println("线程2小了" );
}
if(number
System.out.println("线程2大了");
}
if(number==number3){
System.out.println("线程3正确" );
break;
}
if(number>number3){
System.out.println("线程3小了");
}
if(number
System.out.println("线程3大了" );
}
//lock1.unlock();
synchronized (o) {
o.notifyAll();
}
synchronized (o2) {
try {
o2.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
System.out.println("over");
}
public void init(){
r2 = new Run2();
r3 = new Run3();
r2.setDaemon(true);
r3.setDaemon(true);
executor = Executors.newFixedThreadPool(2);
executor.execute(r2);
executor.execute(r3);
}
}
public class Run2 extends Thread{
/* (non-Javadoc)
* @see java.lang.Thread#run()
*/
@Override
public void run() {
synchronized (o) {
if(number==-1){
try {
o.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
while(true){
Random r = new Random();
//lock1.lock();
number2 = r.nextInt(100);
System.out.println("线程2:"+number2);
// lock1.unlock();
synchronized(o2){
if(count<1){
count++;
}else{
System.out.println("r2 notify" );
o2.notify();
}
System.out.println(count);
}
synchronized (o) {
try {
o.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
public class Run3 extends Thread{
@Override
public void run() {
synchronized (o) {
if(number==-1){
try {
o.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
while(true){
Random r = new Random();
//lock1.lock();
number3 = r.nextInt(100);
System.out.println("线程3:"+number3);
// lock1.unlock();
synchronized(o2){
if(count<1){
count++;
}else{
System.out.println("r3 notify" );
o2.notify();
}
System.out.println(count);
}
synchronized (o) {
try {
o.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
}