java 6种常用控制线程方式整理_java中对于线程的控制方法有哪些

本文介绍了Java中ReentrantLock,Condition,synchronized和Semaphore等并发控制机制在`foo`和`bar`方法中的使用,强调了这些工具在处理多线程顺序执行和同步中的重要性。同时,提倡系统化学习以提升技术能力。
摘要由CSDN通过智能技术生成
public void foo(Runnable printFoo) throws InterruptedException {     
    for (int i = 0; i < n; ) {
        if(permitFoo) {
    	    printFoo.run();
        	i++;
        	permitFoo = false;
        }else{
            Thread.yield();
        }
    }
}

public void bar(Runnable printBar) throws InterruptedException {       
    for (int i = 0; i < n; ) {
        if(!permitFoo) {
    	printBar.run();
    	i++;
    	permitFoo = true;
        }else{
            Thread.yield();
        }
    }
}

}


#### 4. 可重入锁 + Condition



class FooBar4 {
private int n;

public FooBar4(int n) {
    this.n = n;
}
Lock lock = new ReentrantLock(true);
private final Condition foo = lock.newCondition();
volatile boolean flag = true;
public void foo(Runnable printFoo) throws InterruptedException {
    for (int i = 0; i < n; i++) {
        lock.lock();
        try {
        	while(!flag) {
                foo.await();
            }
            printFoo.run();
            flag = false;
            foo.signal();
        }finally {
        	lock.unlock();
        }
    }
}

public void bar(Runnable printBar) throws InterruptedException {
    for (int i = 0; i < n;i++) {
        lock.lock();
        try {
        	while(flag) {
                foo.await();
        	}
            printBar.run();
            flag = true;
            foo.signal();
        }finally {
        	lock.unlock();
        }
    }
}

}


#### 5. synchronized + 标志位 + 唤醒



class FooBar3 {
private int n;
// 标志位,控制执行顺序,true执行printFoo,false执行printBar
private volatile boolean type = true;
private final Object foo= new Object(); // 锁标志

public FooBar3(int n) {
    this.n = n;
}
public void foo(Runnable printFoo) throws InterruptedException {
    for (int i = 0; i < n; i++) {
        synchronized (foo) {
            while(!type){
                foo.wait();
            }
            printFoo.run();
            type = false;
            foo.notifyAll();
        }
    }
}

public void bar(Runnable printBar) throws InterruptedException {
    for (int i = 0; i < n; i++) {
        synchronized (foo) {
            while(type){
                foo.wait();
            }
            printBar.run();
            type = true;
            foo.notifyAll();
        }
    }
}

}


#### 6. 信号量 适合控制顺序



class FooBar2 {
private int n;
private Semaphore foo = new Semaphore(1);
private Semaphore bar = new Semaphore(0);
public FooBar2(int n) {
this.n = n;
}

public void foo(Runnable printFoo) throws InterruptedException {
    for (int i = 0; i < n; i++) {
        foo.acquire();
    	printFoo.run();
        bar.release();
    }
}
public void bar(Runnable printBar) throws InterruptedException {
    for (int i = 0; i < n; i++) {
        bar.acquire();
    	printBar.run();
        foo.release();
    }
}

}

img
img

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化资料的朋友,可以戳这里获取

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

可以戳这里获取](https://bbs.csdn.net/topics/618545628)**

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

  • 3
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值