前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >分布使用synchronized关键字和AtomicInteger进行线程同步的例子

分布使用synchronized关键字和AtomicInteger进行线程同步的例子

作者头像
Jerry Wang
发布2020-04-27 11:56:59
3830
发布2020-04-27 11:56:59
举报
代码语言:javascript
复制
package atomicIntegerTest;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicInteger;

import org.junit.Test;

public class TestAtomic {
	
	 public TestAtomic(){
		 
	 }
	  private static final int MAX_THREADS = 3;
	  private static final int TASK_COUNT = 3;
	  private static final int TARGET_COUNT = 100 * 10000;
	  private AtomicInteger acount = new AtomicInteger(0);
	  private int count = 0;
	  synchronized int inc() {    
	      return ++count;
	  }
	  synchronized int getCount() {    
	      return count;
	  }

	class SyncThread implements Runnable {
	    String name;    
	    long startTime;
	    TestAtomic out;    
	    public SyncThread(TestAtomic o, long startTime) {        		     
	        this.out = o;        
	        this.startTime = startTime;
	    }    
	    @Override
	    public void run() {        
	      int v = out.inc();        
	      // System.out.println("Value after inc: " + v + " Thread id: " + Thread.currentThread().getId());
	      while (v < TARGET_COUNT) {
	            v = out.inc();
	      }        
	      long endTime = System.currentTimeMillis();
	      System.out.println("SyncThread spend:" + (endTime - startTime) + "ms" + ", v=" + v + " Thread id: " + Thread.currentThread().getId());
	    }
	}

	class AtomicThread implements Runnable {
	    String name;    
	    long startTime;    
	    public AtomicThread(long startTime) {        
	       this.startTime = startTime;
	    }    
	    @Override 
	    public void run() {        
	       int v = acount.incrementAndGet();        
	       while (v < TARGET_COUNT) {
	            v = acount.incrementAndGet();
	        }        
	        long endTime = System.currentTimeMillis();
	        System.out.println("AtomicThread spend:" + (endTime - startTime) + "ms" + ", v=" + v + " Thread id: " + Thread.currentThread().getId());
	    }
	}

	@Test
	public void testSync() throws InterruptedException {
	    ExecutorService exe = Executors.newFixedThreadPool(MAX_THREADS);    
	    long startTime = System.currentTimeMillis();
	    SyncThread sync = new SyncThread(this, startTime);    
	    for (int i = 0; i < TASK_COUNT; i++) {
	        exe.submit(sync);
	    }
	    Thread.sleep(1000);
	}

	@Test
	public void testAtomic() throws InterruptedException {
	    ExecutorService exe = Executors.newFixedThreadPool(MAX_THREADS);    
	    long startTime = System.currentTimeMillis();
	    AtomicThread atomic = new AtomicThread(startTime);    
	    for (int i = 0; i < TASK_COUNT; i++) {
	        exe.submit(atomic);
	    }
	    Thread.sleep(1000);
	}
	}

输出:

代码语言:javascript
复制
SyncThread spend:55ms, v=1000000 Thread id: 16
SyncThread spend:55ms, v=1000002 Thread id: 14
SyncThread spend:55ms, v=1000001 Thread id: 15
AtomicThread spend:37ms, v=1000000 Thread id: 19
AtomicThread spend:37ms, v=1000001 Thread id: 17
AtomicThread spend:37ms, v=1000002 Thread id: 18
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2020-04-26 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档