俩个线程一个加1,一个减1 « 关注Java

公告:上个空间遭黑客攻击,刚恢复换了空间,数据全部丢失,我也尽力挽回了部分链接,请各位交换链接的博友,看到后留言给我。

俩个线程一个加1,一个减1

SyndContentImpl.value=

public  class   ThreadTest1
{
private  int j;
public  static  void  main(String  args[])
{
ThreadTest1  tt=new  ThreadTest1();
Inc  inc=tt.new Inc();
Dec  dec=tt.new Dec();
for(int i=0;i<2;i++)
{
Thread  t= new Thread(inc);
t.start();
t=new  Thread(dec);
t.start();
}
}
private  synchronized  void  inc()
{
j++;
System.out.println(Thread.currentThread.getName()+”-inc:”+j);
}

private synchronized  void dec()
{
j–;
System.out.println(Thread.currentThread.getName()+”-dec”+j);
}

class  Inc implements  Runnable{
public  void  run()
{
for(int i=0;i<100;i++)
{
inc();
}
}
}

Class Dec  implements  Runnable{
public  void  run()
{
for(int i=0;i<100;i++)
{
dec();
}
}
}
}

作者: 平安夜 发表于 2012-01-26 01:12 原文链接

评论: 0 查看评论 发表评论


最新新闻:
· Gmail logo 背后的故事(2012-01-26 23:22)
· 得益于Kindle Fire的火爆,Android平板市场份额飙升至39%(2012-01-26 23:21)
· 二月十四情人节愤怒的小鸟登陆Facebook(2012-01-26 22:55)
· 程苓峰:新浪越成功,就离互联网越远。(2012-01-26 22:29)
· Ubuntu将用搜索界面HUD取代菜单(2012-01-26 21:34)

编辑推荐:我是如何学习计算机编程的

网站导航:博客园首页  我的园子  新闻  闪存  博问  知识库

SyndContentImpl.interface=interface com.sun.syndication.feed.synd.SyndContent
SyndContentImpl.type=text/plain

声明:本文采用 BY-NC-SA 协议进行授权,本文链接:俩个线程一个加1,一个减1