Java线程中的资源共享问题
//ThreadDemo01.java
//实现Runnable接口,可以实现资源的共享
class Demo implements Runnable
{
private int ticket=10;
public void fun()
{
while(this.ticket>0)
{
System.out.println(Thread.currentThread().getName()+"在运行,开始售票:"+this.ticket--);
}
}
public void run()
{
this.fun();
}
}
public class ThreadDemo01
{
public static void main(String[] args)
{
//new Demo();
Thread t1=new Thread(new Demo(),"线程类的线程--->");
Thread t2=new Thread(new Demo(),"线程类的线程--->");
Thread t3=new Thread(new Demo(),"线程类的线程--->");
//启动多线程
//同时启动多个多线程
t1.start();
t2.start();
t3.start();
}
}