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
Monitor Directories and File(III)Multi Threads
猜你喜欢
转载自sillycat.iteye.com/blog/1114479
今日推荐
周排行