当前状态:(2013 年 10 月 10 日):已解决。
问题: 我正在研究线程并决定随机执行一些任务。所以谷歌一个,找到了这个作业
说明(消除游戏逻辑,主要目的是专注于线程)我有四个玩家围成一圈,他们之间已经分配了卡片。他们将洗牌(这个任务是为了简单起见,显然游戏逻辑不同),一旦所有的牌都完成了,他们可以举手表示他们已经完成,然后所有人都可以重复这个过程,从技术角度来说,线程将等待其他人完成,然后最后到达或完成的人可以通知其他人继续......
我的代码状态:
- 不同的线程同时进入同步块。
- 一旦一个线程完成他的工作,它应该增加count变量并且count=4当前线程应该通知其他3个等待,最终我想实现happens-before关系。
主类:
public class RunGame implements Runnable{
volatile int count=0;
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
public static void main(String arg[]){
RunGame obj= new RunGame();
Player p1= new Player("Player 1");
Player p2= new Player("Player 2");
Player p3= new Player("Player 3");
Player p4= new Player("Player 4");
Runnable r1 = new ThreadRun(p1,obj);
Runnable r2 = new ThreadRun(p2,obj);
Runnable r3 = new ThreadRun(p3,obj);
Runnable r4 = new ThreadRun(p4,obj);
Thread t1 = new Thread(r1,"Player 1");
Thread t2 = new Thread(r2,"Player 2");
Thread t3 = new Thread(r3,"Player 3");
Thread t4 = new Thread(r4,"Player 4");
t1.start();
t2.start();
t3.start();
t4.start();
}
}
模型:
public class Player {
private String player;
public Player(String name) {
this.player=name;
}
//getter and setter
}
商务课程:
public class PlayerRun implements Runnable{
Player player;
RunGame mainObj;
public PlayerRun(Player player,RunGame main) {
this.player=player;
this.mainObj=main;
}
public void run() {
while(true){
synchronized (mainObj) {
int count=mainObj.getCount();
System.out.println(Thread.currentThread().getName()+"...."+count);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mainObj.setCount(++count);
System.out.println(Thread.currentThread().getName()+" Done...."+count);
}
synchronized (mainObj) {
try {
if(mainObj.getCount()<=3)
mainObj.wait();//current thread will wait till it is awaken by notify.
else if(mainObj.getCount()>3){
System.out.println(Thread.currentThread().getName()+" is last one to enter and awake all");
mainObj.setCount(0);
mainObj.notifyAll();
}
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
System.out.println(Thread.currentThread().getName()+"==============================");
}
}}
输出:
Player 1....0
Player 1 Done....1
Player 4....1
Player 4 Done....2
Player 3....2
Player 3 Done....3
Player 2....3
Player 2 Done....4
Player 2 is last one to enter and awake all
Player 2==============================
Player 2....0
Player 3==============================
Player 4==============================
Player 2 Done....1
Player 1==============================
Player 4....1
Player 4 Done....2
Player 3....2
Player 3 Done....3
Player 1....3
Player 1 Done....4
Player 1 is last one to enter and awake all
Player 1==============================
Player 3==============================
Player 1....0
Player 1 Done....1
Player 4==============================
Player 3....1
Player 2==============================
Player 3 Done....2
Player 2....2
Player 2 Done....3
Player 4....3
Player 4 Done....4
Player 4 is last one to enter and awake all
Player 4==============================
Player 4....0
Player 3==============================
Player 2==============================
Player 4 Done....1
Player 1==============================
Player 2....1
Player 2 Done....2
Player 3....2
Player 3 Done....3
Player 1....3
Player 1 Done....4
Player 1 is last one to enter and awake all
Player 1==============================
Player 3==============================
Player 1....0
Player 1 Done....1
Player 2==============================
Player 4==============================
Player 3....1
Player 3 Done....2
Player 4....2
Player 4 Done....3
Player 2....3
Player 2 Done....4
Player 2 is last one to enter and awake all
Player 2==============================
Player 3==============================
Player 2....0
Player 1==============================
Player 4==============================
Player 2 Done....1
Player 4....1
Player 4 Done....2
Player 1....2
Player 1 Done....3
Player 3....3
Player 3 Done....4
Player 3 is last one to enter and awake all
Player 3==============================
Player 3....0
Player 1==============================
Player 3 Done....1
Player 4==============================
Player 1....1
Player 2==============================
Player 1 Done....2
Player 2....2
Player 2 Done....3
Player 4....3
Player 4 Done....4
Player 4 is last one to enter and awake all
Player 4==============================
Player 2==============================
Player 3==============================
Player 1==============================
Player 4....0
Player 4 Done....1
Player 1....1
Player 1 Done....2
Player 3....2
Player 3 Done....3
Player 2....3
Player 2 Done....4
Player 2 is last one to enter and awake all