乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      JDK1.5新特性--java.util.concurrent BlockingQue...

       goldbomb 2008-01-13

      發(fā)庫中的BlockingQueue是一個比較好玩的類,顧名思義,就是阻塞隊列。該類主要提供了兩個方法put()和take(),前者將一個對象放到隊列中,如果隊列已經(jīng)滿了,就等待直到有空閑節(jié)點;后者從head取一個對象,如果沒有對象,就等待直到有可取的對象。

      下面的例子比較簡單,一個讀線程,用于將要處理的文件對象添加到阻塞隊列中,另外四個寫線程用于取出文件對象,為了模擬寫操作耗時長的特點,特讓線程睡眠一段隨機長度的時間。另外,該Demo也使用到了線程池和原子整型(AtomicInteger),AtomicInteger可以在并發(fā)情況下達到原子化更新,避免使用了synchronized,而且性能非常高。由于阻塞隊列的put和take操作會阻塞,為了使線程退出,特在隊列中添加了一個“標識”,算法中也叫“哨兵”,當發(fā)現(xiàn)這個哨兵后,寫線程就退出。

      當然線程池也要顯式退出了。

      package concurrent;
      import java.io.File;
      import java.io.FileFilter;
      import java.util.concurrent.BlockingQueue;
      import java.util.concurrent.ExecutorService;
      import java.util.concurrent.Executors;
      import java.util.concurrent.LinkedBlockingQueue;
      import java.util.concurrent.atomic.AtomicInteger;

      public class TestBlockingQueue {
        static long randomTime() {
          return (long) (Math.random() 1000);
        }

        public static void main(String[] args) {
          // 能容納100個文件
          final BlockingQueue<File> queue = new LinkedBlockingQueue<File>(100);
          // 線程池
          final ExecutorService exec = Executors.newFixedThreadPool(5);
          final File root = new File("F:\\JavaLib");
          // 完成標志
          final File exitFile = new File("");
          // 讀個數(shù)
          final AtomicInteger rc = new AtomicInteger();
          // 寫個數(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(".java");
                  }
                });
                for (File one : files)
                  scanFile(one);
              else {
                try {
                  int index = rc.incrementAndGet();
                  System.out.println("Read0: " + index + " "
                      + file.getPath());
                  queue.put(file);
                catch (InterruptedException e) {
                }
              }
            }
          };
          exec.submit(read);
          // 四個寫線程
          for (int index = 0; index < 4; index++) {
            // write thread
            final int NO = index;
            Runnable write = new Runnable() {
              String threadName = "Write" + NO;
              public void run() {
                while (true) {
                  try {
                    Thread.sleep(randomTime());
                    int index = wc.incrementAndGet();
                    File file = queue.take();
                    // 隊列已經(jīng)無對象
                    if (file == exitFile) {
                      // 再次添加"標志",以讓其他線程正常退出
                      queue.put(exitFile);
                      break;
                    }
                    System.out.println(threadName + ": " + index + " "
                        + file.getPath());
                  catch (InterruptedException e) {
                  }
                }
              }
            };
            exec.submit(write);
          }
          exec.shutdown();
        }
      }

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

        0條評論

        發(fā)表

        請遵守用戶 評論公約

        類似文章 更多