Monitor Directories and File(III)Multi Threads

本文介绍如何使用多线程进行文件监控,并探讨了线程的基本概念、创建方法及中断处理。此外,还深入讲解了Java中Executor框架的使用方式,以及BlockingQueue的多种实现形式,包括自定义BlockingQueue和利用Lock与Condition来实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值