静态
如果同步函数被静态修饰后,使用的锁是什么呢?
通过验证,发现不是this
因为静态方法中不可以定义this
静态进内存时,内存中没有本类对象,但是一定有给类对应的字节码文件对象。
类名:class 该对象的类型时Class
class Ticket implements Runnable
{
private int tick=100;
public void run()
{
synchronized(Ticket.class)
{
while(true)
{
if(tick>0)
{
System.out.println("tick..."+tick--);
}
}
}
}
}
class StaticDemo
{
public static void main(String[] args)
{
Ticket c=new Ticket();
Thread t1=new Thread(c);
Thread t2=new Thread(c);
t1.start();
t2.start();
}
}