多线程之间的通信与并发库工具

来源:互联网 发布:ipv6根域名中国服务器 编辑:程序博客网 时间:2024/06/14 15:32

传统线程的创建
1,———————-在Thread子类覆盖的run()方法中编写运行代码
Thread thread = new Thread(){
@Override
public void run() {
while(true){
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(“1:” + Thread.currentThread().getName());
System.out.println(“2:” + this.getName());
}
}
};
thread.start();

2,——————创建一个Runnable对象 实现代码在runnable对象里面的run()方法里面写执行代码块 然后将该runnable对象传递给Thread对象执行线程 更加面向对象
Thread thread2 = new Thread(new Runnable(){
@Override
public void run() {
while(true){
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(“1:” + Thread.currentThread().getName());

            }                       }    });    thread2.start();--------当覆盖Thread子类 又给Thread对象创建了runnable对象 先会执行覆盖子类方法的运行代码 当没找到覆盖子类代码的时候 就会去执行runnable对象的run()方法里的运行代码

线程同步

1——–关键词Synchronized互斥 wait() notify() 配套使用使线程同步完成通信

public class TraditionalThreadCommunication {

/** * @param args */public static void main(String[] args) {    final Business business = new Business();    new Thread(            new Runnable() {                @Override                public void run() {                    for(int i=1;i<=50;i++){                        business.sub(i);                    }                }            }    ).start();    for(int i=1;i<=50;i++){        business.main(i);    }}

}
class Business {
private boolean bShouldSub = true;
//该方法所在类的调用对象形成互斥
public synchronized void sub(int i){
while(!bShouldSub){
try {
this.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
for(int j=1;j<=3;j++){
System.out.println(“sub thread sequence of ” + j + “,loop of ” + i);
}
bShouldSub = false;
this.notify();
}
//该方法所在类的调用对象形成互斥
public synchronized void main(int i){
while(bShouldSub){
try {
this.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
for(int j=1;j<=5;j++){
System.out.println(“main thread sequence of ” + j + “,loop of ” + i);
}
bShouldSub = true;
this.notify();
}
}

2———-lock对象形成互斥 lock下的condition await() signal()完成通信 使线程同步
public class ConditionCommunication {
private final static CommonBusiness commonBusiness = new CommonBusiness();
public static void main(String[] args) {
new Thread(new Runnable() {
@Override
public void run() {
for(int i=0;i<20;i++){
commonBusiness.method1(i);
}
}
}).start();
new Thread(new Runnable() {
@Override
public void run() {
for(int i=0;i<20;i++){
commonBusiness.method2(i);
}
}
}).start();
}
}
class CommonBusiness{
private Boolean aBoolean = true;
private Lock lock = new ReentrantLock();
//lock下的condition实现 条件阻塞
Condition condition1 = lock.newCondition();
Condition condition2 = lock.newCondition();
public void method1(int i){
lock.lock();
try {
while(!aBoolean) {
try {
condition1.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
for (int j = 0; j < 5; j++) {
System.out.println(“method1 task:” + j + ” loop of: ” + i);
}
aBoolean = false;
condition2.signal();
}finally {
lock.unlock();
}
}
public void method2(int i){
lock.lock();
try {
while(aBoolean) {
try {
condition2.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
for (int j = 0; j < 4; j++) {
System.out.println(“method2 task:” + j + ” loop of: ” + i);
}
aBoolean = true;
condition1.signal();
} finally {
lock.unlock();
}
}
}

使用ReadWriteLock模拟一个缓存模型
// 模拟一个获取数据的缓存系统
public class CacheData {
private ReadWriteLock readWriteLock = new ReentrantReadWriteLock();
private static Map

原创粉丝点击