|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
最后我再次声明,我并没有说不看好java,实际上我对java很乐观的,毕竟她正在不断改进中,我相信她总有一天会和.net并驾齐驱的
Java代码
/**
* 一段摹拟龟兔竞走的多线程程序。<br>
* 兔子比乌龟快5倍,但歇息的工夫长10倍。
*
* @author 赵学庆,Java世纪网(java2000.net)
*
*/
public class T {
public static void main(String[] args) {
TortoiseRace a = new TortoiseRace();
Thread runner1 = new Thread(a);
RabbitRace b = new RabbitRace();
Thread runner2 = new Thread(b);
runner1.start();
runner2.start();
while (ready < 2) {
try {
Thread.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
synchronized (lock) {
lock.notifyAll();
}
}
static Object lock = new Object();
static int ready = 0;
}
class TortoiseRace implements Runnable {
public void run() {
synchronized (T.lock) {
T.ready++;
try {
T.lock.wait();
} catch (InterruptedException e1) {
e1.printStackTrace();
}
}
int TortoiseDistance = 0;
for (int i = 0; TortoiseDistance <= 1000; i++) {
TortoiseDistance++;
System.out.println("乌龟跑了1米!");
try {
Thread.sleep(1);
} catch (InterruptedException e) {
}
}
System.out.println("乌龟已跑完了竞赛旅程!");
}
}
class RabbitRace implements Runnable {
public void run() {
T.ready++;
synchronized (T.lock) {
try {
T.lock.wait();
} catch (InterruptedException e1) {
e1.printStackTrace();
}
}
int RabbitDistance = 0;
for (int j = 0; RabbitDistance <= 1000; j++) {
RabbitDistance += 5;
System.out.println("兔子跑了5米!");
try {
Thread.sleep(10);
} catch (InterruptedException e) {
}
}
System.out.println("兔子跑完了竞赛旅程!");
}
}
C#是盗用了Java的源代码,仿照开发的,原因是Java是开源的啊,盗了也白盗,还有一点,开发C#语言的团队是就是开发Java语言的团队,是微软重金挖过去的啊 |
|