制作四个线程,共同操作同一个数字,其中2个线程,对该数字进行++操作另外两个线程对该数字进行--操作。


class Data{

    public int j = 0;

}
class Plus implements Runnable{
    private Data data;
    public Plus(Data data) {
        this.data = data;
    }

    @Override
    public void run() {
        while (true){
            synchronized (data){
                try{
                    Thread.sleep(400);
                    data.j++;
                    System.out.println(Thread.currentThread().getName()+":"+data.j);
                }catch (InterruptedException e) {
                    e.printStackTrace();
                }

            }

        }

    }
}
class Sub implements Runnable{
    private Data data;
    public Sub(Data data) {
        this.data = data;
    }

    @Override
    public void run() {
        while(true){
            synchronized (data){
                try{
                    Thread.sleep(400);
                    data.j--;
                    System.out.println(Thread.currentThread().getName()+":"+data.j);
                }catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
public class AppTest {
    public static void main(String[] args) {
        Data data = new Data();
        Plus plus = new Plus(data);
        Sub sub = new Sub(data);
        new Thread(plus,"加线程一").start();
        new Thread(plus,"加线程二").start();
        new Thread(sub,"减线程一").start();
        new Thread(sub,"减线程二").start();
    }
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
这个问题可以通过使用Java多线程编程来实现。首先,需要定义一个可共享的整型变量,即多个线程都可以对其进行修改。然后,创建两个线程两个线程,它们同时对该变量进行法和操作,最后输出变量的值。 以下是一个可能的解决方案: ```java public class Main { public static void main(String[] args) { Counter counter = new Counter(); // 实例化计数器对象 AddThread addThread1 = new AddThread(counter); // 实例化线程对象 AddThread addThread2 = new AddThread(counter); SubThread subThread1 = new SubThread(counter); // 实例化线程对象 SubThread subThread2 = new SubThread(counter); addThread1.start(); // 启动线程 addThread2.start(); subThread1.start(); subThread2.start(); try { addThread1.join(); // 等待线程执行完毕 addThread2.join(); subThread1.join(); subThread2.join(); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("变量的值为:" + counter.getCount()); // 输出变量的值 } } class Counter { private int count = 0; public synchronized void add() { // 操作 count++; } public synchronized void sub() { // 操作 count--; } public int getCount() { // 获取变量值 return count; } } class AddThread extends Thread { private Counter counter; public AddThread(Counter counter) { this.counter = counter; } public void run() { for (int i = 0; i < 10000; i++) { counter.add(); // 进行操作 } } } class SubThread extends Thread { private Counter counter; public SubThread(Counter counter) { this.counter = counter; } public void run() { for (int i = 0; i < 10000; i++) { counter.sub(); // 进行操作 } } } ``` 在上述代码中,定义了一个`Counter`类,它包含一个可共享的整型变量`count`,并提供了法和操作的方法。`AddThread`和`SubThread`类分别表示线程线程,它们在运行时,通过调用`Counter`类的方法来修改变量的值。 需要注意的是,在`Counter`类的方法上上`synchronized`关键字,可以保证多个线程访问该方法时的线程安全性。此外,使用`join()`方法可以等待线程执行完毕后再输出结果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值