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

传统线程的创建
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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值