首页 > 代码库 > java多线程编程(3)买票
java多线程编程(3)买票
1,买票非同步版本
http://www.cnblogs.com/anbylau2130/archive/2013/04/17/3025347.html很详细
1 public class 多线程2 { 2 3 /** 4 * @param args 5 */ 6 public static void main(String[] args) { 7 // TODO Auto-generated method stub 8 Runnable r=new mythread(); 9 Thread t1=new Thread(r);10 Thread t2=new Thread(r);11 t1.start();12 t2.start();13 14 }15 16 }17 class mythread implements Runnable18 {19 private int num=100;20 public void run()21 {22 while(true)23 {24 if(num>0){25 try {26 Thread.sleep(1000);27 } catch (InterruptedException e) {28 // TODO Auto-generated catch block29 e.printStackTrace();30 }31 32 33 System.out.println(Thread.currentThread().getName()+"剩下"+num--);}34 else break;35 36 }37 38 }39 40 41 }
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。