`
sillycat
  • 浏览: 2539118 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

Monitor Directories and File(III)Multi Threads

阅读更多
Monitor Directories and File(III)Multi Threads

Reading a book named shaojinwensj recently and learn a lot from him.
1. Basic ideas
If we want to start a thread, we need to give it a name.

We need to do something if the thread interrupted
If(Thread.interrupted()){
break;
}

try{
doSomething();
}catch(InterruptedException e){
break;
}

if(Thread.interrupted()){
throw new InterruptedException();
}

ThreadLocal<T>  
initialValue
set
get
remove

2. Executors
java.util.concurrent.Executors is the factory class of Executor.
TaskOne.java:
package com.xxxxx.importdata.filemonitor.multithreads;
import java.util.concurrent.Callable;
public class TaskOne implements Callable<Object>
{
    public Object call() throws Exception
    {
        System.out.println("TaskOne is called!");
        return true;
    }
}
TaskTwo.java:
package com.xxxxxx.importdata.filemonitor.multithreads;
public class TaskTwo implements Runnable
{
    public void run()
    {
        System.out.println("TaskTwo is called!");
    }
}
SingleExecutor.java:
package com.xxxxx.importdata.filemonitor.multithreads;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
public class SingleExecutor
{
    public static void main(String[] args) throws InterruptedException, ExecutionException, TimeoutException
    {
        TaskOne taskOne = new TaskOne();
        TaskTwo taskTwo = new TaskTwo();
        ExecutorService executor = Executors.newSingleThreadExecutor();
        Future<Object> future1 = executor.submit(taskOne);
        future1.get();
        Future<?> future2 = executor.submit(taskTwo);
        future2.get(3, TimeUnit.SECONDS);
    }
}

3. Blocking Queue
blockingQ.put(object) block when queue is full------producer
blockingQ.take() block when queue is empty ---------consumer

ArrayBlockingQueue
LinkedBlockingQueue
SynchronousQueue

Queue<E>
add
offer
remove
poll
element
peek

BlockingQueue<E>
put
take
drainTo(Collection)

we will use put and take, better to use drainTo.

Object object = blockingQ.poll();    //not right
Object object = blockingQ.take();  // right
Object object = blockingQ.poll(1,TimeUnit.SECONDS); // right

A simple BlockingQ implementation

package com.xxxxxx.importdata.filemonitor.multithreads;
import java.util.LinkedList;
import java.util.Queue;
public class BlockingQ
{
    private Object        notEmpty   = new Object();
    private Object        notFull    = new Object();
    private Queue<Object> linkedList = new LinkedList<Object>();
    private int           maxLength  = 10;
    public Object take() throws InterruptedException
    {
        synchronized (notEmpty)
        {
            if (linkedList.size() == 0)
            {
                notEmpty.wait();
            }
            synchronized (notFull)
            {
                if (linkedList.size() == maxLength)
                {
                    notFull.notifyAll();
                }
                return linkedList.poll();
            }
        }
    }
    public void offer(Object object) throws InterruptedException
    {
        synchronized (notEmpty)
        {
            if (linkedList.size() == 0)
            {
                notEmpty.notifyAll();
            }
            synchronized (notFull)
            {
                if (linkedList.size() == maxLength)
                {
                    notFull.wait();
                }
                linkedList.add(object);
            }
        }
    }
}

A lock and condition BlockingQueue
package com.xxxxx.importdata.filemonitor.multithreads;
import java.util.LinkedList;
import java.util.Queue;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class BlockingQ
{
    private Lock          lock       = new ReentrantLock();
    private Condition     notEmpty   = lock.newCondition();
    private Condition     notFull    = lock.newCondition();
    private Queue<Object> linkedList = new LinkedList<Object>();
    private int           maxLength  = 10;
    public Object take() throws InterruptedException
    {
        lock.lock();
        try
        {
            if (linkedList.size() == 0)
            {
                notEmpty.await();
            }
            if (linkedList.size() == maxLength)
            {
                notFull.signalAll();
            }
            return linkedList.poll();
        }
        finally
        {
            lock.unlock();
        }
    }
    public void offer(Object object) throws InterruptedException
    {
        lock.lock();
        try
        {
            if (linkedList.size() == 0)
            {
                notEmpty.signalAll();
            }
            if (linkedList.size() == maxLength)
            {
                notFull.await();
            }
            linkedList.add(object);
        }
        finally
        {
            lock.unlock();
        }
    }
}

4. ReentrantLock and Synchronized
Synchronized is a simple Lock, one common Lock will have multi Conditions, but synchronized will be one Lock and
one Condition.

Lock                -----> lock(); unlock();
Condition     -----> await();signal();signalAll()
synchronized ---> lock();unlock();wait();notify();notifyAll();

Atomic
AtomicInteger
AtomicBoolean
AtomicLong
AtomicReference

references:
CountDownLatch
http://www.iteye.com/topic/1002652
http://www.iteye.com/topic/869109
http://www.iteye.com/topic/581476
CyclicBarrier
http://www.iteye.com/topic/657295
http://www.iteye.com/topic/363625
http://www.iteye.com/topic/713053
Pools
http://w2c2y2.iteye.com/blog/479672
http://www.cnblogs.com/jobs
分享到:
评论

相关推荐

    File and Directories

    Linux文件系统是操作系统的一个重要组成部分,它是Unix系统的一个演变,为用户提供了一个虚拟存储的抽象,主要包括文件和目录的概念。文件是存储在Linux文件系统中最基本的单位,它是一个字节序列,这些字节可以通过...

    Reading and sorting directories with C on Linux and Unix

    Directories - Reading and sorting directories. readdir.c - Reading a directory (readdir). dirsortsize.c - Sort a directory by file size (scandir). dirsortalpha.c - Sort a directory alphabetically ...

    VM虚拟机Ubuntu系统打开报错A start job is running for Create Volatile Files and Directories(no limit)解决方案

    报错是因为虚拟机的磁盘用完了,想要正常启动需要进入recovery模式,进去把不需要的文件删除,然后才你那个正常启动。 1.重启Ubuntu,随机长按shift进入grub菜单,或等待grub菜单出现 2.选择Advanced options for ...

    Wiley.Publishing.Fedora.Linux.Toolbox.1000+.Commands.for.Fedora.CentOS.and.Red.Hat.Power.Users.and.Red.Hat.Power.Users.2008.pdf

    - **File Operations**: Creating, moving, copying, and deleting files and directories using commands like `cp`, `mv`, `rm`, and `mkdir`. - **File Permissions**: Understanding and modifying file ...

    Android代码-Material版的文件选择器

    Material File Picker Material file picker library for Android ... .withFilter(Pattern.compile(".*\.txt$")) // Filtering files and directories by file name using regexp .withFilterDirecto

    µC-OS-III 3.06官方用户手册英文版

    手册使用了特定的文档约定(Documentation Conventions),并详细讲解了目录结构(Directories and Files)和应用程序代码(Application Code)的组织方式,以及µC-OS-III的板级支持包(Board Support Package, BSP...

    LDAP Directories Explained_ An Introduction and Analysis_书2.pdf

    本书《LDAP Directories Explained: An Introduction and Analysis》旨在为读者提供一个全面的LDAP入门指南,不仅介绍了LDAP的基本概念和技术细节,还探讨了其在现代信息技术环境中的应用。 #### 二、LDAP与传统...

    解决SVN上传提示Empty directoriesis not supported

    解决SVN上传提示Empty directoriesis not supported 解决SVN上传提示Empty directoriesis not supported

    McGraw-Hill.Linux.The.Complete.Reference.6th.Edition.2008.pdf

    6 Linux Files, Directories, and Archives 115 Part III Desktop 7 The X Window System, Xorg, and Display Managers 145 8 GNOME 169 9 KDE 197 Part IV Linux Software 10 Software Management 219 11 Offi ce ...

    The C programming Language(chm格式完整版)

    Example - Listing Directories Example - A Storage Allocator Appendix A: Reference Manual Introduction Lexical Conventions Syntax Notation Meaning of Identifiers Objects and Lvalues Conversions...

    iis网络服务Internet

    Checks the installation of the FrontPage Extensions and verifies that file permissions and FrontPage-specific directories are correct. Problems relating to missing FrontPage-specific directories and ...

    utl_file包介绍

    UTL_FILE 包的工作机制是这样的:首先要使用 FOPEN 函数,将文件的路径、文件名、以及打开模式的参数传入,然后 Oracle 会到 ALL_DIRECTORIES 视图中查看路径是否已经创建。如果路径和文件名均合法,则该文件被打开...

    VC6.0__directories_默认设置.doc

    在使用Microsoft Visual C++ 6.0(简称VC6.0)进行开发时,正确配置directories设置至关重要,因为这直接影响到编译器能否找到必要的头文件、库文件以及可执行文件,从而顺利编译和链接程序。当安装过程中...

    Advanced Mac OS X Programming

    Directories, File Systems, and Links 13.NSFileManager - Cocoa and the File System 14.Network Programming With Sockets 15.CFRunLoop 16.kqueue and FSEvents 17.Bonjour 18.Multiprocessing 19.Using NSTask ...

    Netdata Detects

    In Linux, everything is treated as a file, which includes hardware devices, directories, and sockets. Each open file is assigned a file descriptor. When a file is closed, its file descriptor is freed ...

    Linux Shell Scripting Cookbook

    Playing with file descriptors and redirection 19 Arrays and associative arrays 25 Visiting aliases 27 Grabbing information about terminal 29 Getting, setting dates, and delays 30 Debugging the script ...

    Android Security- Attacks and Defenses

    They also cover the Android file system, including import directories and files, so readers can perform basic forensic analysis on file system and SD cards. The book includes access to a wealth of ...

    GAMS Python API documentation 24.0

    If external file communication is not an issue in a particular Python application, temporary directories and files will be managed by objects in the namespace. With the exception of GamsWorkspace ...

Global site tag (gtag.js) - Google Analytics