这是一个关于线程加锁和解锁的小程序,理想的输出结果是
kitty is running
kitty is running
kitty is running
kitty is running
doggy is running
doggy is running
doggy is running
doggy is running
但执行结果是无输出信息,在debug模式下看到的是整个程序都在wait状态,而不是对象dog1在wait状态,求大神解释~~public class th4_1{
public static void main(String[] args) throws InterruptedException {
cccc dog=new cccc("doggy");
cccc cat=new cccc("kitty");
Thread dog1 = new Thread(dog);
Thread cat1 = new Thread(cat);
synchronized(dog1){
try {
dog1.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
cat1.start();
dog1.start();
dog1.notify();
}
}
}
public class cccc implements Runnable{
private String id;
public cccc(String str){
id=str;
}
public void run(){
for (int i = 0; i < 4; i++) {
try {
Thread.sleep((int)(1000*Math.random()));
}
catch (InterruptedException e) {
// TODO: handle exception
}
System.out.println(id+" is running");
}
} }Java多线程
kitty is running
kitty is running
kitty is running
kitty is running
doggy is running
doggy is running
doggy is running
doggy is running
但执行结果是无输出信息,在debug模式下看到的是整个程序都在wait状态,而不是对象dog1在wait状态,求大神解释~~public class th4_1{
public static void main(String[] args) throws InterruptedException {
cccc dog=new cccc("doggy");
cccc cat=new cccc("kitty");
Thread dog1 = new Thread(dog);
Thread cat1 = new Thread(cat);
synchronized(dog1){
try {
dog1.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
cat1.start();
dog1.start();
dog1.notify();
}
}
}
public class cccc implements Runnable{
private String id;
public cccc(String str){
id=str;
}
public void run(){
for (int i = 0; i < 4; i++) {
try {
Thread.sleep((int)(1000*Math.random()));
}
catch (InterruptedException e) {
// TODO: handle exception
}
System.out.println(id+" is running");
}
} }Java多线程
解决方案 »
- java String 问题求教
- (100分)if else 很多怎么办????有什么替代的!(主要是字符串判断)
- Java Applet(JDK1.3版本)编写程序,通过AWT创建用户界面,当有其他的网页打开时,通过对话框FileDialog就不可以导入文件了,这是什么问题
- 要達道里面代碼的功能,不用java 2d 有沒有別的思路? 思路就行~
- 初学JAVA,(菜)引号有什么作用
- 请问如何合并两个文件
- 请问JAVA有没有中文的帮助文档?
- ****请你回答:最近java上面有什么动态阿?
- 请问如何从"byte[]"生成一个Image对象?JDK版本1.2!
- 请问:在java中如何实现拷贝文件的功能? 哪位兄弟知道,请告知? 谢谢!
- JTextFiled中内容不可修改怎么做啊
- 有关java和数据库的小问题
cccc dog = new cccc("doggy");
cccc cat = new cccc("kitty");
Thread dog1 = new Thread(dog);
Thread cat1 = new Thread(cat);
cat1.start();
dog1.start();
}
}class cccc implements Runnable {
private String id; public cccc(String str) {
id = str;
} public void run() {
synchronized (JavaSeTest.class) {
for (int i = 0; i < 4; i++) {
try {
Thread.sleep((int) (1000 * Math.random()));
} catch (InterruptedException e) {
}
System.out.println(id + " is running");
}
}
}
public static void main(String[] args) throws InterruptedException {
cccc dog = new cccc("doggy");
cccc cat = new cccc("kitty");
Thread dog1 = new Thread(dog);
Thread cat1 = new Thread(cat);
cat1.start();
dog1.start();
}
}class cccc implements Runnable {
private String id; public cccc(String str) {
id = str;
} public void run() {
synchronized (JavaSeTest.class) {
for (int i = 0; i < 4; i++) {
try {
Thread.sleep((int) (1000 * Math.random()));
} catch (InterruptedException e) {
}
System.out.println(id + " is running");
}
}
}
}
http://www.ticmy.com/?p=219