package com.同步线程与死锁;
public class threadDemo {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Mythread my=new Mythread();
Thread t1=new Thread(my,"汪汪");
Thread t2=new Thread(my,"瞄瞄");
t1.start();
t2.start();
}
}
class Mythread implements Runnable{
Mythread i=new Mythread();//同步的标记对象
public void run(){
//同步代码块
synchronized(i){
System.out.println(Thread.currentThread().getName()+"正在洗澡");
try{
Thread.sleep(1000);
}catch(InterruptedException e){
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+"洗完了");
}
}
}
Exception in thread "main" java.lang.StackOverflowError
at com.同步线程与死锁.Mythread.<init>(threadDemo.java:19)
at com.同步线程与死锁.Mythread.<init>(threadDemo.java:19)
at com.同步线程与死锁.Mythread.<init>(threadDemo.java:19)
........
输出结果怎么会这样?
暧月的猫
慕娘1402314
Coda
迷茫_先森
修缘
陳高高_
相关分类