竞争抢答器
程序员文章站
2022-04-23 15:21:45
/*实现一个竞拍抢答程序:要求设置三个抢答者(三个线程),而后同时发出抢答指令,抢答成功者显示成功提示,抢答失败者给出失败提示。*/ package Java多线程_01; import java.util.concurrent.Callable; import java.util.concurre... ......
/*实现一个竞拍抢答程序:要求设置三个抢答者(三个线程),而后同时发出抢答指令,抢答成功者显示成功提示,抢答失败者给出失败提示。*/
package java多线程_01;
import java.util.concurrent.callable;
import java.util.concurrent.futuretask;
class mythread_08 implements callable<string>{
private boolean flag = false;
@override
public string call() throws exception{
synchronized(this) {
if(this.flag == false) {
this.flag = true;
return thread.currentthread().getname()+"抢答成功!";
}else {
return thread.currentthread().getname()+"抢答失败!";
}
}
}
}
public class homework_03 {
public static void main(string[] args) throws exception{
mythread_08 mt = new mythread_08();
futuretask<string> taska = new futuretask<string>(mt);
futuretask<string> taskb = new futuretask<string>(mt);
futuretask<string> taskc = new futuretask<string>(mt);
new thread(taska, "竞赛者a").start();
new thread(taskb, "竞赛者b").start();
new thread(taskc, "竞赛者c").start();
system.out.println(taska.get());
system.out.println(taskb.get());
system.out.println(taskc.get());
}
}
上一篇: 魏延墓前有三个跪像,他们分别是谁?
下一篇: 一筐团子都睡了