class PO
{
public static void main(String args[])
{
ThreadDemo th = new ThreadDemo();
th.start();
for(int i=0;;i++)
{
System.out.println("main.i="+i);
wait();
notify();
}
}
}
class ThreadDemo extends Thread
{
public void run()
{
for(int i= 0;;i++)
{
System.out.println("ThreadDemo.i="+i);
wait();
notify();
}
}
}
{
public static void main(String args[])
{
ThreadDemo th = new ThreadDemo();
th.start();
for(int i=0;;i++)
{
System.out.println("main.i="+i);
wait();
notify();
}
}
}
class ThreadDemo extends Thread
{
public void run()
{
for(int i= 0;;i++)
{
System.out.println("ThreadDemo.i="+i);
wait();
notify();
}
}
}
解决方案 »
- 怎么在一个java项目中使用自己的包呢?
- 没见过的JAVA代码写法
- 经典递增问题,大家来讨论!
- 关于JAVA Object 反序列化的问题,如何将一个格式化的String反序列化成对应的Java object
- Java操作EXCEL时为什么会抛NullPointerException??? 在线求救~~~~~~
- 如果提交多条SQL语句
- 为什么需要拖动一下才会显示窗口中的控件?
- 急寻西安电子科技大学出版社 《面向对象程序设计——java》电子书
- 谁能把vc++的一段代码,转成java
- 如何防止按浏览器右上角的"x" 来关闭浏览器
- 小弟请教各位大虾!!!感激不尽!!!
- 有谁能详细解释下生产消费者问题,包括原理和代码。
class PO
{
public static void main(String args[])
{
ThreadDemo th = new ThreadDemo();
th.start();
PO p = new PO();
for(int i=0;;i++)
{
System.out.println("main.i="+i);
try{
p.wait();
p.notify();
}
catch(Exception e){
} }
}
}
class ThreadDemo extends Thread
{
public void run()
{
for(int i= 0;;i++)
{
System.out.println("ThreadDemo.i="+i);
try{
wait();
notify();
}
catch(Exception e)
{}
}
}
}
假如还有第三,第四个线程,它就不知道唤醒谁了。
1.wait,notify,notifyAll方法必须放在synchronized内。
2.wait需要异常处理
3.wait,notity,notifyAll是非静态方法,不能直接用在main里面
class P0
{
public static void main(String args[])
{
ThreadDemo th = new ThreadDemo();
th.start();
for(int i=0;;i++)
{
System.out.println("main.i="+i);
try {
new P0().wait();
new p0().notify();
} catch (InterruptedException e) {
e.printStackTrace();
}
new P0().notify();
}
}
}
class ThreadDemo extends Thread
{
public void run()
{
for(int i= 0;;i++)
{
System.out.println("ThreadDemo.i="+i);
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
notify();
}
}
}
运行起好像有些问题,我也不懂这个干嘛的???
notifyAll()是把所有这wait()的10个进程都唤醒! 这里只是从wait()状态出来到就绪状态哈,至于执行,跟这个又没有关系了.
是通过对象来的.wait 和notify 是对象的方法. class PO
{
public static void main(String args[]) throws Exception
{
ThreadDemo th = new ThreadDemo();
ThreadDemo th2 = new ThreadDemo();
th2.start();
th.start();
Thread.sleep(100);
synchronized(th2){
for(int i=0;i< 10;i++)
{
System.out.println("main.i="+i);
th2.notify();//这里只是notify的th2对象的wait状态.
}
}
}
}
class ThreadDemo extends Thread
{
static int idCount = 0;
int id;
ThreadDemo(){
idCount++;
id = idCount;
}
public void run()
{synchronized(this){
// for(int i= 0;i<1;i++)
// {
System.out.println("th.id " + this.id);
try {
this.wait();
System.out.println("th.id " + this.id);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// notify();这句话没有意义吧.前面都wait了,这里notify执行不到这来啊.
// 要是前面没有wait那你这个notify谁啊
}
}
// }
}通过输出看到它并没有唤醒 把两个thread都唤醒.
th.id 2
th.id 1
main.i=0
main.i=1
main.i=2
main.i=3
main.i=4
main.i=5
main.i=6
main.i=7
main.i=8
main.i=9
th.id 2
想要全部唤醒用notifyAll, notify 唤醒取决于虚拟机的调度机制,无法通过程序操控
不确定的,
而是让他们去竞争。