多线程题目——打印零与奇偶数

在这里插入图片描述
好像LC上大部分(就那几道)多线程题目都是考察等待通知机制。
这题要求0奇0偶的顺序打印直至n.
那就得构造屏障嘛,保证它们能够交替允许,信号量Semaphore就能很好的完成这个工作。
设一个变量为公共变量,记录当前应该打印的奇偶数,在0方法里递增。别忘了再最后唤醒它们去结束等待。
代码如下:

Semaphore

class ZeroEvenOdd {
    private int n;
    Semaphore zero = new Semaphore(1);
    Semaphore even = new Semaphore(0);
    Semaphore odd = new Semaphore(0);
    public ZeroEvenOdd(int n) {
        this.n = n;
    }
    private volatile int count =1;
    // printNumber.accept(x) outputs "x", where x is an integer.
    public void zero(IntConsumer printNumber) throws InterruptedException {
        while(count<=n){
            zero.acquire();
            if(count>n){
                even.release();
                odd.release();
                break;
            }
            printNumber.accept(0);
            //System.out.println("\nzero:"+count);
            if((count&1)==0){
                even.release();
            }else {
                odd.release();
            }
        }
    }

    public void even(IntConsumer printNumber) throws InterruptedException {
        while(count<=n){
            even.acquire();
            if(count>n)break;
            //System.out.println("\neven:"+count);
            printNumber.accept(count);
            count++;
            zero.release();
        }
    }

    public void odd(IntConsumer printNumber) throws InterruptedException {
        while(count<=n){
            odd.acquire();
            if(count>n)break;
            //System.out.println("\nodd:"+count);
            printNumber.accept(count);
            count++;
            zero.release();
        }
    }
}

ReentrantLock+Condition

那么还有其他方法构建屏障吗?当然可以使用等待队列。可以使用Lock也可以使用默认的synchronized,不过Lock的优势在于可以建立多个等待队列,每次只唤醒对应的,这样可以减少锁竞争。

class ZeroEvenOdd {
   private int n;
    private volatile boolean  flag = true;
    private ReentrantLock lock = new ReentrantLock();
    private Condition zero = lock.newCondition();
    private Condition even = lock.newCondition();
    private Condition odd = lock.newCondition();
    public ZeroEvenOdd(int n) {
        this.n = n;
    }
    private volatile int count =1;
    // printNumber.accept(x) outputs "x", where x is an integer.
    public void zero(IntConsumer printNumber) throws InterruptedException {
        while(count<=n){
            lock.lock();
            try {
                while(!flag) {
                    zero.await();
                }
                flag = false;
                if (count > n) {
                    even.signalAll();
                    odd.signalAll();
                    break;
                }
                printNumber.accept(0);
                //System.out.println("\nzero:"+count);
                
            }finally {
                if((count & 1) == 0) {
                    even.signal();
                } else {
                    odd.signal();
                }
                lock.unlock();
            }
        }
    }

    public void even(IntConsumer printNumber) throws InterruptedException {
        while(count<=n){
            lock.lock();
            try{
                while(flag||(count&1)==1){
                    even.await();
                }
                flag = true;
                if(count>n)break;
                //System.out.println("\neven:"+count);
                printNumber.accept(count);
                count++;
            }finally {
                zero.signal();
                lock.unlock();
            }
        }
    }

    public void odd(IntConsumer printNumber) throws InterruptedException {
        while(count<=n){
            lock.lock();
            try{
                while(flag||(count&1)==0){
                    odd.await();
                }
                flag = true;
                if(count>n)break;
                //System.out.println("\neven:"+count);
                printNumber.accept(count);
                count++;
            }finally {
                zero.signal();
                lock.unlock();
            }
        }
    }
}

sychronized

还可以使用sychronized的moniter锁,同理,不过竞争明显多于上一种,效率会没那么高

class ZeroEvenOdd {
    private int n;
    private int count = 1;
    private boolean isZeroPrinted = false;

    public ZeroEvenOdd(int n) {
        this.n = n;
    }

    // printNumber.accept(x) outputs "x", where x is an integer.
    public void zero(IntConsumer printNumber) throws InterruptedException {
        while (count < n + 1) {
            synchronized (this) {
                if (isZeroPrinted) {
                    this.wait();
                } else {
                    printNumber.accept(0);
                    isZeroPrinted = true;
                    this.notifyAll();
                }
            }
        }
    }

    public void even(IntConsumer printNumber) throws InterruptedException {
        while (count < n + 1) {
            synchronized (this) {
                if (!isZeroPrinted || (count &1) == 1) {
                    this.wait();
                } else {
                    printNumber.accept(count++);
                    isZeroPrinted = false;
                    this.notifyAll();
                }
            }
        }
    }

    public void odd(IntConsumer printNumber) throws InterruptedException {
        while (count < n + 1) {
            synchronized (this) {
                if (!isZeroPrinted || (count &1) == 0) {
                   this.wait();
                } else {
                    printNumber.accept(count++);
                    isZeroPrinted = false;
                    this.notifyAll();
                }
            }
        }
    }
}

无锁,状态量比较(各方法单线程时)

如果每个方法都是单个线程执行的话,那么可以采用状态量的方式。如下所示:

class ZeroEvenOdd {
    private int n;
    private volatile int count = 1;
    private volatile int stage = 0;
    public ZeroEvenOdd(int n) {
        this.n = n;
    }
    // printNumber.accept(x) outputs "x", where x is an integer.
    public void zero(IntConsumer printNumber) throws InterruptedException {
        while (count < n + 1) {
            if (stage!=0) {
                Thread.yield();
            } else {
                printNumber.accept(0);
                stage = (count&1)==0?1:2;
            }
        }
    }
    public void even(IntConsumer printNumber) throws InterruptedException {
        while (count < n + 1) {
            if (stage!=1) {
                Thread.yield();
            } else {
                printNumber.accept(count++);
                stage = 0;
            }
        }
    }

    public void odd(IntConsumer printNumber) throws InterruptedException {
        while (count < n + 1) {
            if (stage!=2) {
               Thread.yield();
            } else {
                printNumber.accept(count++);
                stage = 0;
            }
        }
    }
}

无锁,状态量比较(各方法多线程时)

如果是都是多线程的话,可以考虑带版本号的原子量。由于我们只在意有没有有改过,并不在意修改了几次,所以我们可以使用AtomicMarkableReference

class ZeroEvenOdd {
   private int n;
    private volatile int count = 1;
    private AtomicMarkableReference<Integer> stage = new AtomicMarkableReference<>(0,false);
    public ZeroEvenOdd(int n) {
        this.n = n;
    }

    // printNumber.accept(x) outputs "x", where x is an integer.
    public void zero(IntConsumer printNumber) throws InterruptedException {
        while (count < n + 1) {
            if (!this.stage.compareAndSet(0,0,false,true)) {
                Thread.yield();
            } else {
                printNumber.accept(0);
                if((count&1)==1){
                    this.stage.set(2,false);
                }else {
                    this.stage.set(1,false);
                }
            }

        }
    }

    public void even(IntConsumer printNumber) throws InterruptedException {
        while (count < n + 1) {
            if (!this.stage.compareAndSet(1,1,false,true)) {
                Thread.yield();
            } else {
                printNumber.accept(count++);
                this.stage.set(0,false);
            }
        }
    }

    public void odd(IntConsumer printNumber) throws InterruptedException {
        while (count < n + 1) {
            if (!this.stage.compareAndSet(2,2,false,true)) {
               Thread.yield();
            } else {
                printNumber.accept(count++);
                this.stage.set(0,false);
            }
        }
    }
}

阻塞队列(任务单线程时)

使用阻塞队列当队列满时,put需要等待一个该队列的take才能继续执行。
以zero方法作为生产者,其他两个方法作为消费者。
不过这么写也有个弊端,没有竞争到那个take的名额的线程就会一直阻塞于take,又判断不了是否已经结束了。也就是说只适用于每个方法的执行线程只有一个的场景。不过这本来也不是生产者/消费者的情景。

 private final int n,e,o;
    private volatile int count =1;
    private BlockingQueue<Integer> evens = new ArrayBlockingQueue<>(1);
    private BlockingQueue<Integer> odds = new ArrayBlockingQueue<>(1);
    private BlockingQueue<Integer> zeros = new ArrayBlockingQueue<>(1);
    public ZeroEvenOdd(int n) {
        this.n = n;
        e = (n&1)==0?n:n-1;
        o = e==n?n-1:n;
    }

    // printNumber.accept(x) outputs "x", where x is an integer.
    public void zero(IntConsumer printNumber) throws InterruptedException {
        while(count<n+1){
            zeros.put(0);
            printNumber.accept(0);
            if((count&1)==1){
                odds.put(count++);
            }else {
                evens.put(count++);
            }
        }
    }

    public void even(IntConsumer printNumber) throws InterruptedException {
        while (count<e+1){
            int ans = evens.take();
            printNumber.accept(ans);
            zeros.take();
        }

    }

    public void odd(IntConsumer printNumber) throws InterruptedException {
        while (count<o+1){
            int ans = odds.take();
            printNumber.accept(ans);
            zeros.take();
        }

    }
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在 C++ 中,我们可以使用线程库来实现多线程编程。线程的挂起、唤醒与终止是多线程编程中非常重要的一部分。 线程的挂起也称为线程的休眠,它可以让线程停止运行一段时间,等待某个条件满足后再继续运行。在 C++ 中,我们可以使用 std::this_thread::sleep_for() 函来实现线程的挂起,该函可以让当前线程挂起一段时间,例如: ```cpp #include <chrono> #include <thread> int main() { // 挂起当前线程 1 秒钟 std::this_thread::sleep_for(std::chrono::seconds(1)); return 0; } ``` 线程的唤醒可以通过条件变量来实现,条件变量是一种同步机制,用于在线程之间传递信号。在 C++ 中,我们可以使用 std::condition_variable 类来创建条件变量,然后使用 wait() 函来挂起线程等待条件变量的信号,使用 notify_one() 函来唤醒一个等待条件变量的线程,例如: ```cpp #include <condition_variable> #include <mutex> #include <thread> std::condition_variable cv; std::mutex mtx; bool ready = false; void worker_thread() { // 等待条件变量的信号 std::unique_lock<std::mutex> lock(mtx); cv.wait(lock, [](){ return ready; }); // 条件满足后继续执行 // ... } int main() { // 唤醒等待条件变量的线程 { std::lock_guard<std::mutex> lock(mtx); ready = true; } cv.notify_one(); return 0; } ``` 线程的终止可以使用 std::thread::join() 函来实现,该函可以让当前线程等待另一个线程执行完成后再继续执行,例如: ```cpp #include <thread> void worker_thread() { // ... } int main() { std::thread t(worker_thread); // 等待 worker_thread 执行完成 t.join(); return 0; } ``` 另外,线程的终止还可以使用 std::thread::detach() 函来实现,该函可以让当前线程与创建的线程分离,使得两个线程可以独立运行,例如: ```cpp #include <thread> void worker_thread() { // ... } int main() { std::thread t(worker_thread); // 分离线程,使得两个线程可以独立运行 t.detach(); return 0; } ``` 需要注意的是,分离线程后,主线程不能再使用 join() 函等待子线程执行完成,否则会导致程序崩溃。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值