synchronized public void run(){
for(int i=0;i<10;i++){
System.out.println(currentThread().getName()+" "+i);
try{sleep(Math.round(Math.random()*1000));}
catch(InterruptedException e){}}}
public static void main(String args[]){
a x=new a();
a y=new a();
x.setName("线程1");
y.setName("线程2");
x.start();
y.start();}}
上面的程序线程1会不会在sleep的时候释放对象锁、
(System.out.println()语句会使线程放弃cup吗
for(int i=0;i<10;i++){
System.out.println(currentThread().getName()+" "+i);
try{sleep(Math.round(Math.random()*1000));}
catch(InterruptedException e){}}}
public static void main(String args[]){
a x=new a();
a y=new a();
x.setName("线程1");
y.setName("线程2");
x.start();
y.start();}}
上面的程序线程1会不会在sleep的时候释放对象锁、
(System.out.println()语句会使线程放弃cup吗
wait 的话就会暂时释放掉锁住的对象,其他的线程可以访问该对象。