RELATEED CONSULTING
相关咨询
选择下列产品马上在线沟通
服务时间:8:30-17:00
你可能遇到了下面的问题
关闭右侧工具栏

新闻中心

这里有您想知道的互联网营销解决方案
使用Runnable实现数据共享-创新互联

使用Runnable实现数据共享,供大家参考,具体内容如下

网站设计制作、网站设计,成都做网站公司-创新互联建站已向上1000+企业提供了,网站设计,网站制作,网络营销等服务!设计与技术结合,多年网站推广经验,合理的价格为您打造企业品质网站。

先上代码:

public class TestThread {
  private static final Logger logger = LoggerFactory.getLogger(TestThread.class);

  private final class MyRunnable implements Runnable {
    private int i;
    public MyRunnable() {
      this.i = 10;
    }
    public void run() {
      while(i > 0) {
        synchronized (this) {
          if (i > 0) {
            i--;
            logger.debug("{} buy one ticket, {} left. ", Thread.currentThread().getName(), i);
          }
        }
      }
    }
  }

  @Test
  public void testRunable() throws InterruptedException{
    MyRunnable myRunnable = new MyRunnable();
    Thread th2 = new Thread(myRunnable);
    Thread th3 = new Thread(myRunnable);
    th2.start();
    th3.start();
    th2.join();
    th3.join();
  }
}

标题名称:使用Runnable实现数据共享-创新互联
网址分享:http://sczitong.cn/article/djseds.html