Concurrent Thread

1. Runnable && Callable

Callable is an interface similar to Runnable. What's the difference is call() in Callable have a return value, while run() in Runnable is void.
The call() method must be invoked by ExecutorService.submit();

ExecutorService executorService = Executors.newCachedThreadPool();
ArrayList> results = new ArrayList>();
for(int i = 0; i < 5; i++){
    results.add(execotrService.submit(new Thread1(i)));     // submit will return an object of Future.
}
for(Future fs : results){
    try{
        System..out.println(fs.get());    // get() blocks until completion.
    }catch(Exception e){

    }finally{
        executorService.shutdown();
    }
}

/**********/
public class Thread1 implements Callable {
    private int id;
    public Thread1(int id){
        this.id = id;
    }
    
    public String call(){
        return "Thread1 id: " + id;
    }
}

2. yield()

Similar to sleep(), to halt current thread and get back to the status of runnable(If getting CPU, the status of thread will be running.), give CPU to other thread that is in the same priority. But it can not ensure the time as sleep() can. CPU will decide which thread to execute.
yield() is unreliable.

3. synchronized

An object has one and only one lock. All the synchronized methods share one lock.
**Principle: **when should I use synchronized?
If a variable will be read(written) by a thread, and written(read) by another thread, then you need to add synchronized. And read thread and write thread must use the same lock.

***Attention: *** Who gets the lock, who can visit the synchronized resource, and the other synchronzied resources in the same object(can visit the memory of synchronized).

4. Lock

Explicitly acquire a lock.
Lock can do any clean work when exceptions. While synchronized just throw a exception.
Lock can try to acquire a lock by lock.tryLock() or lock.tryLock(long, TimeUnit) and give it up by lock.unlock() in finally block, while synchronized can not.

// In a class, you can get a lock instance.
private Lock lock = new ReentrantLock();

public int classMethod1(){
    lock.lock();            // acquire a lock explicitly.
    // must use try...finally...
    try{
        //method body. Do sth...
        return value;      // return statement must be before finally.
    }finally{
        lock.unlock();     // In finally, do some clean work and unlock.
    }
}

你可能感兴趣的:(Concurrent Thread)