Java的多线程技术

已知有ABCD四个线程,有变量i,其中线程AB对i进行加1,线程CD对i进行减1,四个线程顺序执行,每个线程每次只执行一次。i的初始值为0,打印结果0121012101……

  1. Variable类

    • 变量 i 初始值为0。
    • threadNumber 用于控制线程执行的顺序。
    • 使用 ReentrantLock 和 Condition 来管理线程的执行和等待。
  2. incrementA方法

    • 当 threadNumber 为1时,线程A加1并打印变量i,然后设置threadNumber为2,唤醒所有等待线程。
  3. incrementB方法

    • 当 threadNumber 为2时,线程B加1并打印变量i,然后设置threadNumber为3,唤醒所有等待线程。
  4. decrementC方法

    • 当 threadNumber 为3时,线程C减1并打印变量i,然后设置threadNumber为4,唤醒所有等待线程。
  5. decrementD方法

    • 当 threadNumber 为4时,线程D减1并打印变量i,然后设置threadNumber为1,唤醒所有等待线程。
  6. ThreadA、ThreadB、ThreadC、ThreadD类

    • 分别代表四个线程,每个线程在循环中不断调用对应的方法。
  7. SequenceExecution类

    • 创建并启动四个线程。

通过以上实现,四个线程将按照顺序执行并对变量 i 进行加减操作,从而实现打印结果0121012101……

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

class Variable {
    private int i = 0;
    private int threadNumber = 1;
    private final Lock lock = new ReentrantLock();
    private final Condition condition = lock.newCondition();

    public void incrementA() {
        lock.lock();
        try {
            while (threadNumber != 1) {
                condition.await();
            }
            i++;
            System.out.println(i);
            threadNumber = 2;
            condition.signalAll();
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        } finally {
            lock.unlock();
        }
    }

    public void incrementB() {
        lock.lock();
        try {
            while (threadNumber != 2) {
                condition.await();
            }
            i++;
            System.out.println(i);
            threadNumber = 3;
            condition.signalAll();
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        } finally {
            lock.unlock();
        }
    }

    public void decrementC() {
        lock.lock();
        try {
            while (threadNumber != 3) {
                condition.await();
            }
            i--;
            System.out.println(i);
            threadNumber = 4;
            condition.signalAll();
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        } finally {
            lock.unlock();
        }
    }

    public void decrementD() {
        lock.lock();
        try {
            while (threadNumber != 4) {
                condition.await();
            }
            i--;
            System.out.println(i);
            threadNumber = 1;
            condition.signalAll();
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        } finally {
            lock.unlock();
        }
    }
}

class ThreadA extends Thread {
    private final Variable variable;

    public ThreadA(Variable variable) {
        this.variable = variable;
    }

    public void run() {
        while (true) {
            variable.incrementA();
        }
    }
}

class ThreadB extends Thread {
    private final Variable variable;

    public ThreadB(Variable variable) {
        this.variable = variable;
    }

    public void run() {
        while (true) {
            variable.incrementB();
        }
    }
}

class ThreadC extends Thread {
    private final Variable variable;

    public ThreadC(Variable variable) {
        this.variable = variable;
    }

    public void run() {
        while (true) {
            variable.decrementC();
        }
    }
}

class ThreadD extends Thread {
    private final Variable variable;

    public ThreadD(Variable variable) {
        this.variable = variable;
    }

    public void run() {
        while (true) {
            variable.decrementD();
        }
    }
}

public class SequenceExecution {
    public static void main(String[] args) {
        Variable variable = new Variable();
        ThreadA threadA = new ThreadA(variable);
        ThreadB threadB = new ThreadB(variable);
        ThreadC threadC = new ThreadC(variable);
        ThreadD threadD = new ThreadD(variable);

        threadA.start();
        threadB.start();
        threadC.start();
        threadD.start();
    }
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值