Java线程同步问题之警察抓小偷
2010-11-19 21:41
429 查看
public class policemanAndThief {
int x, y;
Boolean isCatched = false;
/**
* */
public static void main(String[] args) {
policemanAndThief pt = new policemanAndThief();
Policeman p = pt.new Policeman("YiJichangkong");
Thief t = pt.new Thief("Liu du du");
p.start();
t.start();
}
public class Policeman extends Thread {
Policeman() {
super();
}
Policeman(String s) {
super(s);
}
public void run() {
while (true) {
for (x = -100; x <= 100; x += (int) (Math.random() * 6 + 1)) {
System.out.println("Police " + this.getName() + " are at: "
+ x);
/*
* try { this.sleep((int)(Math.random()3000+1000)); } catch
* (InterruptedException e) { e.printStackTrace(); }
*/
Catch();
}
}
}
}
public class Thief extends Thread {
public Thief(String s) {
super(s);
}
public void run() {
while (true) {
for (y = -100; y <= 100; y += (int) (Math.random() * 6 + 1)) {
System.out.println("Thief " + this.getName() + " are At: "
+ y);
/*
* try { this.sleep(1000); } catch (InterruptedException e)
* { e.printStackTrace(); }
*/
Catch();
}
}
}
}
public void Catch() {
if (x >= -10 && x <= 10 && y >= -10 && y <= 10) {
System.out.println("police at: " + x + " thief at: " + y);
System.out.println("The thief is catched!");
System.exit(0);
}
}
}
int x, y;
Boolean isCatched = false;
/**
* */
public static void main(String[] args) {
policemanAndThief pt = new policemanAndThief();
Policeman p = pt.new Policeman("YiJichangkong");
Thief t = pt.new Thief("Liu du du");
p.start();
t.start();
}
public class Policeman extends Thread {
Policeman() {
super();
}
Policeman(String s) {
super(s);
}
public void run() {
while (true) {
for (x = -100; x <= 100; x += (int) (Math.random() * 6 + 1)) {
System.out.println("Police " + this.getName() + " are at: "
+ x);
/*
* try { this.sleep((int)(Math.random()3000+1000)); } catch
* (InterruptedException e) { e.printStackTrace(); }
*/
Catch();
}
}
}
}
public class Thief extends Thread {
public Thief(String s) {
super(s);
}
public void run() {
while (true) {
for (y = -100; y <= 100; y += (int) (Math.random() * 6 + 1)) {
System.out.println("Thief " + this.getName() + " are At: "
+ y);
/*
* try { this.sleep(1000); } catch (InterruptedException e)
* { e.printStackTrace(); }
*/
Catch();
}
}
}
}
public void Catch() {
if (x >= -10 && x <= 10 && y >= -10 && y <= 10) {
System.out.println("police at: " + x + " thief at: " + y);
System.out.println("The thief is catched!");
System.exit(0);
}
}
}
相关文章推荐
- Java笔记3 多线程<1>线程概述、多线程的创建、多线程的安全问题、静态同步函数的锁、死锁
- java static 与 线程 共用同步问题
- Java线程:线程的同步与锁 一、同步问题提出 线程的同步是为了防止多个线程访问一个数据对象时,对数据造成的破坏。 例如:两个线程ThreadA、ThreadB都操作同一个对象Foo对
- Java线程之锁对象Lock-同步问题更完美的处理方式代码实例
- Java线程(八):锁对象Lock-同步问题更完美的处理方式
- java中同步synchronized的意义,如何用它解决线程不安全的问题
- Java线程:线程的同步问题
- [java线程同步]生产者消费者问题demo
- Java线程(八):锁对象Lock-同步问题更完美的处理方式(转)
- Java线程(八):锁对象Lock-同步问题更完美的处理方式
- Java线程间通信的同步问题、wait/notify使用
- Java线程基本用法与同步问题
- java#3 java线程的同步问题
- java线程同步问题——由腾讯笔试题引发的风波
- Java线程(七):锁对象Lock-同步问题更完美的处理方式 .
- Java线程(七):锁对象Lock-同步问题更完美的处理方式
- Java线程同步卖票问题解决方法
- Java线程(八):锁对象Lock-同步问题更完美的处理方式
- Java线程(八)----锁对象Lock-同步问题更完美的处理方式
- Android(java)学习笔记70:同步中的死锁问题以及线程通信问题