总的来说:java多线程最终都是回归到了Thread和Runnable。
常见的多线程实现方式:
1:实现Runnable接口,复写run方法。最后调用的时候,还是通过Thread的start来启动。
2:继承Thread方法,复写run方法。鉴于java的单继承方式,直接继承Thread用的较少。
3:实现Callable< T >接口,复写call方法,注意,此方法与runnable方法不同之处是,可以返回参数。
eg:
public class CallableTest implements Callable<String>{
@Override
public String call() throws Exception{
System.out.println("测试");
}
public static void main(){
Callable oneCall = new CallableTest();
FutureTask<String> oneTask = new FutureTask(onCall);
new Thread(oneTask).start();
ExecutorService pool = Excutors.newCachedThreadPool();
Future future = pool.submit(oneCall);
System.out.println(future.get());
pool.shutdown();
}
}
4:线程池,交给线程池运行
ExecutorService pool = Executors.newCachedThreadPool();
pool.submit();//pool.execute();
注意:submit可以接受runnable、callable。execute只是runnable。
记住:pool.shutdown();
注意:stop和shutdown的区别,stop是一种强终止,会破坏逻辑原子性。而shutdown则是较为温和,安全的关闭线程。
线程同步实现。
1:volatile:可见性,对内存可见(直接写入内存,而非先放入缓存器或者寄存器)。常用来定义共享的变量。
eg:private volatile int num = 0;
2:Atomic(原子性,详见juc的atomic包),Integer需要另外加锁才可以实现安全性,AtomicInteger确保原子性。
3:synchronized:原子性,可见性,不可中断,非公平锁。
4:lock:可中断、可公平(或非公平)。
出自juc:
ReadWriteLock(读写锁)实现了lock接口
public class ReentrantLock implements Lock, java.io.Serializable
创建对象:
Lock lock = new ReentrantLock();
或者是
ReentrantLock lock = new ReentrantLock();
ReentrantLock(重入锁)、ReentrantReadWriteLock(重入读写锁)实现了ReadWriteLock接口。
public class ReentrantReadWriteLock
implements ReadWriteLock, java.io.Serializable
创建对象:
ReadWriteLock lock = new ReentrantReadWriteLock();或者
ReentrantReadWriteLock = new ReentrantReadWriteLock();
读锁:lock.readLock().lock();
写锁:lock.writeLock().lock();
重入锁在上一篇有讲过java-ReentrantLock(重入锁)+Condition-解决生产者,消费者模型
读写锁:读与读之间互不影响;读与写、写与写之间互斥。
读锁并发,读写互斥,上代码
public class ReadAndWriteLock {
ReentrantReadWriteLock locks = new ReentrantReadWriteLock();
ReentrantReadWriteLock rrwl = new ReentrantReadWriteLock();
public void get(Thread thread){
try{
locks.readLock().lock();
//System.out.println(thread.currentThread().getName()+" "+System.currentTimeMillis());
for(int i=0;i<5;i++){
System.out.println(thread.currentThread().getName()+"读操作");
Thread.sleep(20);
}
}catch(Exception e){
e.printStackTrace();
}finally{
locks.readLock().unlock();
}
}
public void readFile(){
rrwl.readLock().lock();
boolean available = rrwl.isWriteLocked();
if(!available){
try{
System.out.println("当前是可读操作");
for(int i=0;i<5;i++){
System.out.println(Thread.currentThread().getName()+"正在进行读操作...");
Thread.sleep(20);
}
}catch(Exception e){
e.printStackTrace();
}finally{
rrwl.readLock().unlock();
}
}
}
public void writeFile(){
rrwl.writeLock().lock();
boolean available = rrwl.isWriteLocked();
if(available){
try{
System.out.println("当前是可写操作");
for(int i=0;i<5;i++){
System.out.println(Thread.currentThread().getName()+" 正在进行写操作...");
Thread.sleep(20);
}
}catch(Exception e){
e.printStackTrace();
}finally{
rrwl.writeLock().unlock();
}
}
}
public static void main(String[] args) {
ReadAndWriteLock rwl = new ReadAndWriteLock();
/*
读锁并发检验
new Thread(()-> rwl.get(Thread.currentThread())).start();
new Thread(()-> rwl.get(Thread.currentThread())).start();
*/
ExecutorService service = Executors.newCachedThreadPool();
service.submit((Runnable)()->rwl.writeFile());
service.submit((Runnable)()->rwl.readFile());
service.shutdown();
}
}