java线程 notify无法解除 wait阻塞 java线程waitnotify 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 synchronized (this) { System.out.println("---------------------"); this.wait(); System.out.println("++++++++++++++++++++++"); }你这个线程wait的时候,已经锁住了this对象了,public void jixu(int num) { this.num = num; synchronized (this) { this.notify(); } }所以这里的代码根本就进不去,notify肯定起不到效果了。 public class OutputThreadTest implements Runnable { public void run() { test2 test2 = new test2(); test2.start(); try { Thread.sleep(1000); //在这睡1s //如果不睡,test2.jixu(22)可能在test2这个线程启动前,就已经执行了 } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } test2.jixu(22); } public static void main(String[] args) { Thread thread1 = new Thread(new OutputThreadTest()); thread1.start(); }}class test2 extends Thread { private int num = 0; public void jixu(int num) { this.num = num; synchronized (this) { this.notify(); } } public void run() { try { while (true) { synchronized (this) { System.out.println("---------------------"); this.wait(); System.out.println("++++++++++++++++++++++"); } System.out.println(num); } } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } }} wait notify 配合使用和synchronized一样的效果你都使用,有点逗 我的建议,用了synchronized就去掉wait和notify,要么去掉synchronized,你试试 使用javassist时发现的几个问题,望高手解答。 java 中如何中断执行中的sql语句 Spring的MultiActionController使用ParameterMethodNameResolver遇到的问题 高分求助 在开发项目中,不管是开发还是系统运行,log是必不可少的。怎样利用log4g做一个便于程序员调用的,内容丰富的log 怎样获得HTML 的属性名?用JAVA代码写出来 请教:权限的锁定控制问题 是不是用xdoclet生成,类里必须要有一个id字段? 客户如何调用EJB? 启动tomcat 报错 坐等大侠指教 grid问题求解 关于ajax,服务器返回中文时乱码
synchronized (this) {
System.out.println("---------------------");
this.wait();
System.out.println("++++++++++++++++++++++");
}你这个线程wait的时候,已经锁住了this对象了,public void jixu(int num) {
this.num = num;
synchronized (this) {
this.notify();
}
}所以这里的代码根本就进不去,notify肯定起不到效果了。
public class OutputThreadTest implements Runnable {
public void run() {
test2 test2 = new test2();
test2.start();
try {
Thread.sleep(1000);
//在这睡1s
//如果不睡,test2.jixu(22)可能在test2这个线程启动前,就已经执行了
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
test2.jixu(22);
} public static void main(String[] args) {
Thread thread1 = new Thread(new OutputThreadTest());
thread1.start();
}
}class test2 extends Thread {
private int num = 0; public void jixu(int num) {
this.num = num;
synchronized (this) {
this.notify();
}
} public void run() {
try {
while (true) {
synchronized (this) {
System.out.println("---------------------");
this.wait();
System.out.println("++++++++++++++++++++++");
}
System.out.println(num);
}
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} }
}
你都使用,有点逗