/*
类锁:类只有一个,所以锁是类级别的,只有一个
*/
public class fuck11{
public class static void main(String[] args){
processor p=new processor();
Thread t1=new Thread(p);
t1.setName("t1");
Thread t2=new Thread(p);
t2.setName("t2");
t1.start();
//延迟,保证t1线程先执行
Thread.sleep(1000);
t2.start();
}
}
class processor implements Runnable{
public void run(){
if(Thread.currentThread().getName().equals("t1") ){
myclass.m1();
}
if(Thread.currentThread().getName().equals("t2") ){
myclass.m2();
}
}
}
class myclass {
//synchronized添加到静态方法上,线程执行的时候会找类锁
public synchronized static void m1(){
try{Thread.sleep(10000);}catch(InterruptedException e){}
System.out.println("m1````");
}
public static void m2(){
System.out.println("m2````");
}
}
类锁:类只有一个,所以锁是类级别的,只有一个
*/
public class fuck11{
public class static void main(String[] args){
processor p=new processor();
Thread t1=new Thread(p);
t1.setName("t1");
Thread t2=new Thread(p);
t2.setName("t2");
t1.start();
//延迟,保证t1线程先执行
Thread.sleep(1000);
t2.start();
}
}
class processor implements Runnable{
public void run(){
if(Thread.currentThread().getName().equals("t1") ){
myclass.m1();
}
if(Thread.currentThread().getName().equals("t2") ){
myclass.m2();
}
}
}
class myclass {
//synchronized添加到静态方法上,线程执行的时候会找类锁
public synchronized static void m1(){
try{Thread.sleep(10000);}catch(InterruptedException e){}
System.out.println("m1````");
}
public static void m2(){
System.out.println("m2````");
}
}