ava中为什么通过实现runnabale的线程中,a不用加static,这不是两个不同的线程吗,加上static才能共享吧,为什么不用加?
public class ThreadDemo
{
public static void main(String[] args)
{
MyThread mt1 = new MyThread();
MyThread mt2 = new MyThread();
Thread t1 = new Thread(mt1);
Thread t2 = new Thread(mt2);
t1.start();
t2.start();
}
}
class MyThread implements Runnable
{
private int ticket = 10;
String a="1";//String a=new ("1");这个地方如果用new必须是static,为什么a="1"就不需要static?
public void run()
{
synchronized(a){
for(int i = 0; i < 20; i ++)
{
if(this.ticket > 0)
{
System.out.println("卖票:" + this.ticket--);
}
}
}
} |
|