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();
?? }
}
轉載于:https://www.cnblogs.com/freehell/archive/2009/12/22/2355304.html
總結
以上是生活随笔為你收集整理的Java线程中的资源共享问题的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Windows系统进程全解剖
- 下一篇: JEECG社区 一个微信教育网站案例源码