让出cpu,不让出锁
package thread;
/*
* sleep()让出cpu,不让出锁
*/
public class Thread_8 {
public static void main(String[] args) throws InterruptedException {
final Mythod m = new Mythod();
Thread t1 = new Thread(new Runnable() {
@Override
public void run() {
System.out.println("t1运行");
m.m1();
}
});
Thread t2 = new Thread(new Runnable() {
@Override
public void run() {
System.out.println("t2运行");
m.m2();
}
});
t1.start();
Thread.sleep(100);
t2.start();
}
}
class Mythod{
public synchronized void m1() {
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
for(int i=0;i<5;i++){
System.out.println(Thread.currentThread().getName()+" "+i);
}
}
public synchronized void m2() {
for(int i=0;i<5;i++){
System.out.println(Thread.currentThread().getName()+" "+i);
}
}
}
请关注“知了堂学习社区”,地址:http://www.zhiliaotang.com/portal.php