欢迎访问悦橙教程(wld5.com),关注java教程。悦橙教程  java问答|  每日更新
页面导航 : > > 文章正文

java AtomicLong 示例,javaatomiclong,import java.

来源: javaer 分享于  点击 6709 次 点评:286

java AtomicLong 示例,javaatomiclong,import java.


import java.util.concurrent.atomic.AtomicLong;public class AtomicCounter extends Thread {    private static AtomicLong counter=new AtomicLong(10);    public static void main(String[] args) throws Exception {        AtomicCounter thread1=new AtomicCounter();        thread1.start();        AtomicCounter thread2=new AtomicCounter();        thread2.start();        AtomicCounter thread3=new AtomicCounter();        thread3.start();    }    @Override    public void run() {        Thread t=Thread.currentThread();        long id=t.getId();        for(long i=0; i<10; i++) {            long nextLong=counter.incrementAndGet();            System.out.println(nextLong +"\t"+id);            Sleep(10);         }    }    public static void Sleep(int ms) {        try {            Thread.sleep(ms);            } catch (Exception e) {                System.out.println(e.getMessage());        }    }}
相关栏目:

用户点评