小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

AtomicInteger類的理解與使用

 Levy_X 2017-12-20

首先看兩段代碼,一段是Integer的,一段是AtomicInteger的,為以下:

public class Sample1 { private static Integer count = 0; synchronized public static void increment() { count ; } }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

以下是AtomicInteger的:

public class Sample2 { private static AtomicInteger count = new AtomicInteger(0); public static void increment() { count.getAndIncrement(); } }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

以上兩段代碼,在使用Integer的時(shí)候,必須加上synchronized保證不會(huì)出現(xiàn)并發(fā)線程同時(shí)訪問的情況,而在AtomicInteger中卻不用加上synchronized,在這里AtomicInteger是提供原子操作的,下面就對(duì)這進(jìn)行相應(yīng)的介紹。


AtomicInteger介紹

AtomicInteger是一個(gè)提供原子操作的Integer類,通過線程安全的方式操作加減。


AtomicInteger使用場(chǎng)景

AtomicInteger提供原子操作來進(jìn)行Integer的使用,因此十分適合高并發(fā)情況下的使用。


AtomicInteger源碼部分講解

public class AtomicInteger extends Number implements java.io.Serializable { private static final long serialVersionUID = 6214790243416807050L; // setup to use Unsafe.compareAndSwapInt for updates private static final Unsafe unsafe = Unsafe.getUnsafe(); private static final long valueOffset; static { try { valueOffset = unsafe.objectFieldOffset (AtomicInteger.class.getDeclaredField('value')); } catch (Exception ex) { throw new Error(ex); } } private volatile int value;
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

以上為AtomicInteger中的部分源碼,在這里說下其中的value,這里value使用了volatile關(guān)鍵字,volatile在這里可以做到的作用是使得多個(gè)線程可以共享變量,但是問題在于使用volatile將使得VM優(yōu)化失去作用,導(dǎo)致效率較低,所以要在必要的時(shí)候使用,因此AtomicInteger類不要隨意使用,要在使用場(chǎng)景下使用。


AtomicInteger實(shí)例使用

以下就是在多線程情況下,使用AtomicInteger的一個(gè)實(shí)例,這段代碼是借用IT宅中的一段代碼。

public class AtomicTest { static long randomTime() { return (long) (Math.random() * 1000); } public static void main(String[] args) { // 阻塞隊(duì)列,能容納100個(gè)文件 final BlockingQueue<File> queue = new LinkedBlockingQueue<File>(100); // 線程池 final ExecutorService exec = Executors.newFixedThreadPool(5); final File root = new File('D:\\ISO'); // 完成標(biāo)志 final File exitFile = new File(''); // 原子整型,讀個(gè)數(shù) // AtomicInteger可以在并發(fā)情況下達(dá)到原子化更新,避免使用了synchronized,而且性能非常高。 final AtomicInteger rc = new AtomicInteger(); // 原子整型,寫個(gè)數(shù) final AtomicInteger wc = new AtomicInteger(); // 讀線程 Runnable read = new Runnable() { public void run() { scanFile(root); scanFile(exitFile); } public void scanFile(File file) { if (file.isDirectory()) { File[] files = file.listFiles(new FileFilter() { public boolean accept(File pathname) { return pathname.isDirectory() || pathname.getPath().endsWith('.iso'); } }); for (File one : files) scanFile(one); } else { try { // 原子整型的incrementAndGet方法,以原子方式將當(dāng)前值加 1,返回更新的值 int index = rc.incrementAndGet(); System.out.println('Read0: ' index ' ' file.getPath()); // 添加到阻塞隊(duì)列中 queue.put(file); } catch (InterruptedException e) { } } } }; // submit方法提交一個(gè) Runnable 任務(wù)用于執(zhí)行,并返回一個(gè)表示該任務(wù)的 Future。 exec.submit(read); // 四個(gè)寫線程 for (int index = 0; index < 4; index ) { // write thread final int num = index; Runnable write = new Runnable() { String threadName = 'Write' num; public void run() { while (true) { try { Thread.sleep(randomTime()); // 原子整型的incrementAndGet方法,以原子方式將當(dāng)前值加 1,返回更新的值 int index = wc.incrementAndGet(); // 獲取并移除此隊(duì)列的頭部,在元素變得可用之前一直等待(如果有必要)。 File file = queue.take(); // 隊(duì)列已經(jīng)無對(duì)象 if (file == exitFile) { // 再次添加'標(biāo)志',以讓其他線程正常退出 queue.put(exitFile); break; } System.out.println(threadName ': ' index ' ' file.getPath()); } catch (InterruptedException e) { } } } }; exec.submit(write); } exec.shutdown(); } }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87

AtomicInteger使用總結(jié)

AtomicInteger是在使用非阻塞算法實(shí)現(xiàn)并發(fā)控制,在一些高并發(fā)程序中非常適合,但并不能每一種場(chǎng)景都適合,不同場(chǎng)景要使用使用不同的數(shù)值類。

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多