2013年(92)
分类: 信息化
2013-02-26 05:31:01
[代码] 多线程操练 package cn.thread;
public class Through_out_bridge {
public static void main(String[] args) {
Bridge b = Bridge.getInstance();//实例化桥
//实例化左端9自己,此刻所有人都不能过桥,桥的能够状况象征为不能够用
for ; (int i = 1; i <= 9; i ) {
Thread t = new Thread(new Person(false, i, b));
t.start();
}
//实例化右端12自己,此刻所有人都不能过桥,桥的能够状况象征为不能够用
for( int i=1 ;i<=12;i )
{
Thread t = new Thread(new Person(true,i,b));
t.start();
}
//桥的可用状况给左端第10自己,能够自定义给谁
b.state = true;
Thread t = new Thread(new Person(false, 10, b));
}
}
class Person implements Runnable {
public Bridge bridge;//桥
private String hand;//在桥哪一端
int No;//序号
public Person(boolean side, int i, Bridge bridge) {
this.No = i;
this.bridge = bridge;
if(bridge.state)
{
System.out.println(i "现已过桥。");
}
if (side) {
this.hand = new String("右");
} else {
this.hand = new String("左");
}
}
//过桥办法
public synchronized void through() throws InterruptedException {
if (bridge.state) {
System.out.println(hand "边第" No "在过桥");
bridge.open( No);
} else {
bridge.lock(No);
}
}
public void run() {
try {
Thread.sleep(1000);
// System.out.println(No hand " 边现已过桥!");
through();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
class Bridge {
//可用状况判别true:可用
public boolean state = false;
//自行实例化
public static Bridge getInstance() {
return new Bridge();
}
public synchronized void open(int i) throws InterruptedException {
if (state) {
Thread.sleep(1000);
this.state=true;
notify();
}
}
public synchronized void lock(int i) throws InterruptedException {
if (!state) {
this.state=false;
System.out.println(i " 在等候.");
wait();
}
}
} ;