java 同步代码块锁和同步函数锁

版权声明:所有版权归静待花开所有 https://blog.csdn.net/qq_42081709/article/details/80281616




/*
同步函数的使用的锁是this;


同步函数和同步代码块的区别:
同步函数的锁是固定的this。


同步代码块的锁是任意的对象。


建议使用同步代码块。




*/
class Ticket implements Runnable
{
private  int num = 100;
// Object obj = new Object();
boolean flag = true;
public void run()
{
System.out.println("this:"+this);


if(flag)
while(true)
{
synchronized(this)
{
if(num>0)
{
try{Thread.sleep(10);}catch (InterruptedException e){}
System.out.println(Thread.currentThread().getName()+".....obj...."+num--);
}
}
}
else
while(true)
this.show();
}


public synchronized void show()
{
if(num>0)
{
try{Thread.sleep(10);}catch (InterruptedException e){}

System.out.println(Thread.currentThread().getName()+".....function...."+num--);
}
}
}


class SynFunctionLockDemo 
{
public static void main(String[] args) 
{
Ticket t = new Ticket();
// System.out.println("t:"+t);


Thread t1 = new Thread(t);
Thread t2 = new Thread(t);


t1.start();
try{Thread.sleep(100);}catch(InterruptedException e){}
t.flag = false;
t2.start();
}
}

猜你喜欢

转载自blog.csdn.net/qq_42081709/article/details/80281616