java多线程的(hello world)
来源:互联网 发布:淘宝上的组装电脑 编辑:程序博客网 时间:2024/05/16 16:27
public class MyThreadApp implements Runnable {
// produce
// consume
Thread produce = null;
Thread consume = null;
public void run() {
while (true) {
say();
}
}
/*
* synchronized 同步
* 只能有一个线程使用这个方法,别的线程都要等待
*/
public synchronized void say()
{
if (Thread.currentThread() == produce)
System.out.println("produce");
else if (Thread.currentThread() == consume)
System.out.println("consume");
else
System.out.println("main");
try {
Thread.sleep(600);
notifyAll(); //唤起所有的线程进行竞争。
} catch (InterruptedException e) {
}
}
public void go() {
produce = new Thread(this);
consume = new Thread(this);
produce.start();
consume.start();
}
public static void main(String[] args) {
System.out.println("main method started......");
MyThreadApp myThreadApp = new MyThreadApp();
myThreadApp.go();
myThreadApp.run();
System.out.println("gone!");
}
}
// produce
// consume
Thread produce = null;
Thread consume = null;
public void run() {
while (true) {
say();
}
}
/*
* synchronized 同步
* 只能有一个线程使用这个方法,别的线程都要等待
*/
public synchronized void say()
{
if (Thread.currentThread() == produce)
System.out.println("produce");
else if (Thread.currentThread() == consume)
System.out.println("consume");
else
System.out.println("main");
try {
Thread.sleep(600);
notifyAll(); //唤起所有的线程进行竞争。
} catch (InterruptedException e) {
}
}
public void go() {
produce = new Thread(this);
consume = new Thread(this);
produce.start();
consume.start();
}
public static void main(String[] args) {
System.out.println("main method started......");
MyThreadApp myThreadApp = new MyThreadApp();
myThreadApp.go();
myThreadApp.run();
System.out.println("gone!");
}
}
- java多线程的(hello world)
- JAVA 的HELLO WORLD
- JAVA的Hello World
- Applet的Hello World程序(java)
- java的hello world出错
- 我的JAVA"Hello World"
- JAVA的Hello World程序
- 【MongoDb】MongoDb的hello world实例(Java版)
- java环境变量的配置&Hello world!
- java的第一个程序 ----Hello World
- 学习JAVA的第一天 | Hello World
- 01 java语言编程的hello world
- 我的第一个Java“Hello World!”
- java编程典型的Hello World!!!
- java版RabbitMQ的hello world实现
- Java实例教程(2)Hello World应用程序
- 第一章 JAVA入门(Hello World)
- thrift java 例子入门(hello world)
- 24点
- 关于Oracle的系统函数nvl(x1,x2)!
- explain plan初接触
- 总结[zz]
- const 和 readonly 的区别
- java多线程的(hello world)
- 最简单最明白的关于“事件”和“委托”的例子说明
- 第一次开博 写个发下
- 元数据
- 您可能是盗版软件的受害者——解决办法
- 读书习惯的改变
- 校园屠杀、劫后余生
- Eclipse语言插件安装
- 第二章 COM对象和接口