public class Car extends Thread {
static int[] a = { 0, 0, 0 };
static boolean isFull = false;
private String s; public Car(String s) {
super();
this.s = s;
}
public static void carIn(){
if (a[0] + a[1] + a[2] == 0) {
for (int i = 0; i < a.length; i++) {
a[i] = i + 1;
System.out.println(i + 1 + "号车进入" + i + "位置");
}
} else {
for (int i = 0; i < a.length; i++) {
if(i<2){
a[i] = a[i+1] ;
System.out.println(a[i+1] + "号车进入" + i + "位置");
}else{
a[i]=a[i]+1;
System.out.println(a[i]+1 + "号车进入" + i + "位置");
}
}
}
isFull = true;
} public static void carOut(){
System.out.println(a[0] + "号车从0号位置离开了");
a[0]=0;
isFull = false;
}
public void run() {
if (s.equals("in")) {
while (true) {
synchronized (this) {
if (isFull == true) {
notifyAll();
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
} else {
carIn();
}
}
}
} else {
while (true) {
synchronized (this) {
if (isFull == false) {
notifyAll();
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
} else {
carOut();
}
}
} }
} public static void main(String[] args) {
Car c1 = new Car("in");
Car c2 = new Car("out");
c1.start();
c2.start();
}
}
static int[] a = { 0, 0, 0 };
static boolean isFull = false;
private String s; public Car(String s) {
super();
this.s = s;
}
public static void carIn(){
if (a[0] + a[1] + a[2] == 0) {
for (int i = 0; i < a.length; i++) {
a[i] = i + 1;
System.out.println(i + 1 + "号车进入" + i + "位置");
}
} else {
for (int i = 0; i < a.length; i++) {
if(i<2){
a[i] = a[i+1] ;
System.out.println(a[i+1] + "号车进入" + i + "位置");
}else{
a[i]=a[i]+1;
System.out.println(a[i]+1 + "号车进入" + i + "位置");
}
}
}
isFull = true;
} public static void carOut(){
System.out.println(a[0] + "号车从0号位置离开了");
a[0]=0;
isFull = false;
}
public void run() {
if (s.equals("in")) {
while (true) {
synchronized (this) {
if (isFull == true) {
notifyAll();
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
} else {
carIn();
}
}
}
} else {
while (true) {
synchronized (this) {
if (isFull == false) {
notifyAll();
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
} else {
carOut();
}
}
} }
} public static void main(String[] args) {
Car c1 = new Car("in");
Car c2 = new Car("out");
c1.start();
c2.start();
}
}
解决方案 »
- 除了web安装ActiveX,还有什么方式可以安装
- 如何实现这个界面的显示??
- 看orelly‘s NIO 中的问题
- 求一个JAVA写的FTP客户端源代码
- 如何设计用过一次就废弃的号码 就像是手机充值
- 哪里有oracle 9i的下载,提供者100分。
- 如何用java操作domino数据库
- 如何作连接池
- Design patterns, elements of reusable software
- 一个问题,JRE和JDK什么关系,为什么程序在JDK可以运行,还要JRE
- Runtime.getRuntime和暴风影音问题! 高手进!!
- 怎么通过一个ID树形结构递归找出所有ID,并把这些ID写入一个list
楼主【CLK1982】截止到2008-07-09 09:57:55的历史汇总数据(不包括此帖):
发帖的总数量:0 发帖的总分数:0 每贴平均分数:0
回帖的总数量:0 得分贴总数量:0 回帖的得分率:0%
结贴的总数量:0 结贴的总分数:0
无满意结贴数:0 无满意结贴分:0
未结的帖子数:0 未结的总分数:0
结贴的百分比:---------------------结分的百分比:---------------------
无满意结贴率:---------------------无满意结分率:---------------------
如何结贴请参考这里:http://topic.csdn.net/u/20080501/09/ef7ba1b3-6466-49f6-9d92-36fe6d471dd1.html
public class Car {
int[] a = { 0, 0, 0 };
boolean isFull = false;
String s; public Car() {
} public void carIn() {
if (a[0] + a[1] + a[2] == 0) {
for (int i = 0; i < a.length; i++) {
a[i] = i + 1;
System.out.println(i + 1 + "in " + i + "location");
}
} else {
for (int i = 0; i < a.length; i++) {
if (i < 2) {
a[i] = a[i + 1];
System.out.println(a[i + 1] + "in " + i + "location");
} else {
a[i] = a[i] + 1;
System.out.println(a[i] + 1 + "in " + i + "location");
}
}
}
isFull = true;
} public void carOut() {
System.out.println(a[0] + "out form 0 ");
a[0] = 0;
isFull = false;
} public static void main(String[] args) throws InterruptedException {
Car c1 = new Car();
c1.setS("in");
new T(c1).start();
Thread.sleep(2000);
c1.setS("out");
new T(c1).start();
} public void setS(String s) {
this.s = s;
}
}
class T extends Thread {
Car car = null;
T(Car car) {
this.car = car;
}
public void run() {
if (car.s.equals("in")) {
while (true) {
synchronized (car) {
if (car.isFull == true) {
try {
car.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
} else {
car.carIn();
car.notifyAll();
}
}
}
} else {
while (true) {
synchronized (car) {
if (car.isFull == false) {
try {
car.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
} else {
car.carOut();
car.notifyAll();
}
}
} }
}
}