测试过了,没问题。基本思路,实例化一个桥类,谁得到桥的可用标志谁过桥。
我第一个看到这个100分的,说实话,知道你是个学生要代码而已,线程类好久没练手了,练习一下而已,否则真不会给你写代码。因为我读书的时候也发过类似的求助,知道什么感受。不懂的时候真的没办法,所以告诉你思路。
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();
}
}
}
public class Baidu{
private Bridge bridge = Bridge.getBridge();
public static void main(String[] args){
Baidu tmp = new Baidu();
for(int i=1;i<=12;++i){
new Thread(tmp.new Person("N"+i)).start();
}
for(int i=1;i<=10;++i){
new Thread(tmp.new Person("S"+i)).start();
}
}
private class Person implements Runnable{
private String name;
public Person(String name){
this.name = name;
}
public void run(){
synchronized(bridge){
System.out.println(getName()+" is on the "+Bridge.NAME+",going to the "+getDirection()+".");
}
}
public String getName(){
return name;
}
public String getDirection(){
return name.startsWith("N")?"South":"North";
}
}
}
class Bridge{
private static final Bridge bridge = new Bridge();
public static final String NAME = "BaiDu Bridge";
private Bridge(){}
public static Bridge getBridge(){
return bridge;
}
}
package a;
public class Bridge {
static int s=10;
static int n=12;
boolean available=false;
public Bridge(){}
public synchronized void swalk(){
System.out.println(Thread.currentThread().getName()+"在过桥,向北走。");
try{
Thread.currentThread().destroy();
}catch(Exception e){}
s--;
}
public synchronized void nwalk(){
System.out.println(Thread.currentThread().getName()+"在过桥,向南走。");
try{
Thread.currentThread().destroy();
}catch(Exception e){}
n--;
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Bridge bridge=new Bridge();
SouthP[] sp=new SouthP[10];
NorthP[] np=new NorthP[12];
for(int i=0;i<10;i++){
sp[i]=new SouthP(bridge);
synchronized(bridge){
sp[i].start();
}
}
for(int j=0;j<12;j++){
np[j]=new NorthP(bridge);
synchronized(bridge){
np[j].start();
}
}
}
}
class SouthP extends Thread{
Bridge bridge=new Bridge();
public SouthP(){}
public SouthP(Bridge b){
this.bridge=b;
}
public void run(){
while(bridge.s>0)
bridge.swalk();
}
}
class NorthP extends Thread{
Bridge bridge=new Bridge();
public NorthP(){}
public NorthP(Bridge b){
this.bridge=b;
}
public void run(){
while(bridge.n>0)
bridge.nwalk();
}
}
虽然运行的时候出现红色 但是思路没问题,你把抛出异常再加工一下就可以了。 用每个对象数组的元素,代替一个线程。
象这类不是问技术,而是要代码的,我基本只是留个言,100分,值钱么?哎
对于鱼和渔,LZ是想要鱼拉!!!!!