java线程问题,如何修改代码,让程序能够正常输出如下结果?

发布于 2022-09-06 09:27:23 字数 1355 浏览 14 评论 0

package com.elyong.whatsup;
/**
 * Created by ely ong on 2017/12/5.
 */
public class TestThreads{
    public static void main(String[] args){
        ThreadOne t1=new ThreadOne();
        ThreadTwo t2=new ThreadTwo();
        Thread one=new Thread(t1);
        Thread two=new Thread(t2);
        one.start();
        two.start();
    }
}
class Accum{
    private static Accum a=new Accum();
    private int counter=0;
    private Accum(){}
    public static Accum getAccum(){
        return a;
    }
    public void updateCounter(int add){
        counter+=add;
    }
    public int getCount(){
        return counter;
    }
}
class ThreadOne implements Runnable{
    Accum a=Accum.getAccum();
    public void run(){
        for(int x=0;x<98;x++){
            a.updateCounter(1000);
            try{
                Thread.sleep(50);
            }catch (InterruptedException ex){}
        }
        System.out.println("one "+a.getCount());
    }
}
class ThreadTwo implements Runnable{
    Accum a=Accum.getAccum();
    public void run(){
        for(int x=0;x<99;x++){
            a.updateCounter(1);
            try{
                Thread.sleep(50);
            }catch (InterruptedException ex){}
        }
        System.out.println("two "+a.getCount());
    }
}

输出结果:

one 98098
two 98099

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

空‖城人不在 2022-09-13 09:27:23
package com.elyong.whatsup;

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * Created by ely ong on 2017/12/5.
 */
public class TestThreads {
    public static void main(String[] args) {
        CyclicBarrier barrier = new CyclicBarrier(2);
        ThreadOne t1 = new ThreadOne(barrier);
        ThreadTwo t2 = new ThreadTwo(barrier);
        Thread one = new Thread(t1);
        Thread two = new Thread(t2);
        one.start();
        two.start();
    }
}

class Accum {
    private static Accum a = new Accum();
    //private int counter = 0;

    private AtomicInteger counter;

    private Accum() {
        counter = new AtomicInteger(0);

    }

    public static Accum getAccum() {
        return a;
    }

    public void updateCounter(int add) {
        //counter += add;
        counter.addAndGet(add);
    }

    public int getCount() {
        //return counter;
        return counter.get();
    }
}

class ThreadOne implements Runnable {
    Accum a = Accum.getAccum();

    CyclicBarrier barrier;

    public ThreadOne(CyclicBarrier barrier) {
        this.barrier = barrier;
    }

    public void run() {
        for (int x = 0; x < 98; x++) {
            a.updateCounter(1000);
            try {
                Thread.sleep(50);
            } catch (InterruptedException ex) {
            }
        }
        try {
            barrier.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (BrokenBarrierException e) {
            e.printStackTrace();
        }
        System.out.println("one " + a.getCount());
    }
}

class ThreadTwo implements Runnable {
    Accum a = Accum.getAccum();

    CyclicBarrier barrier;

    public ThreadTwo(CyclicBarrier barrier) {
        this.barrier = barrier;
    }

    public void run() {
        for (int x = 0; x < 99; x++) {
            a.updateCounter(1);
            if(x == 97) {
                try {
                    barrier.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } catch (BrokenBarrierException e) {
                    e.printStackTrace();
                }
            }
            try {
                Thread.sleep(50);
            } catch (InterruptedException ex) {
            }
        }
        System.out.println("two "+a.getCount());

    }
}
沫尐诺 2022-09-13 09:27:23

参考楼上,给出我的两种实现。看起来没那么高大上,但是好理解一些。
实现1:

import java.util.concurrent.atomic.AtomicInteger;

/**
 * Created by ely ong on 2017/12/5.
 */
public class TestThreads {
    public static void main(String[] args) {
        ThreadOne t1 = new ThreadOne();
        ThreadTwo t2 = new ThreadTwo();
        Thread one = new Thread(t1);
        Thread two = new Thread(t2);
        one.start();
        two.start();
    }
}

class Accum {
    private static Accum a = new Accum();
    //private int counter = 0;
    private AtomicInteger counter;
    
    private Accum() {
        counter = new AtomicInteger(0);
    }

    public static Accum getAccum() {
        return a;
    }

    public void updateCounter(int add) {
        //counter += add;
        counter.addAndGet(add);
    }

    public int getCount() {
        //return counter;
        return counter.get();
    }
}

class ThreadOne implements Runnable {
    Accum a = Accum.getAccum();

    public void run() {
        for (int x = 0; x < 98; x++) {
            a.updateCounter(1000);
            try {
                Thread.sleep(50);
            } catch (InterruptedException ex) {
            }
        }
        System.out.println("one " + a.getCount());
    }
}

class ThreadTwo implements Runnable {
    Accum a = Accum.getAccum();

    public void run() {
        for (int x = 0; x < 99; x++) {
            a.updateCounter(1);
            try {
                Thread.sleep(50);
            } catch (InterruptedException ex) {
            }
            if (x == 97) {
                try {
                    Thread.sleep(50);
                } catch (InterruptedException ex) {
                }
            }
        }
        System.out.println("two " + a.getCount());
    }
}

实现二:

/**
 * Created by ely ong on 2017/12/5.
 */
public class TestThreads {
    public static void main(String[] args) {
        ThreadOne t1 = new ThreadOne();
        ThreadTwo t2 = new ThreadTwo();
        Thread one = new Thread(t1);
        Thread two = new Thread(t2);
        one.start();
        two.start();
    }
}

class Accum {
    private static Accum a = new Accum();
    private int counter = 0;

    private Accum() {
    }

    public static Accum getAccum() {
        return a;
    }

    public synchronized void updateCounter(int add) {
        counter += add;
    }

    public int getCount() {
        return counter;
    }
}

class ThreadOne implements Runnable {
    Accum a = Accum.getAccum();

    public void run() {
        for (int x = 0; x < 98; x++) {
            a.updateCounter(1000);
            try {
                Thread.sleep(50);
            } catch (InterruptedException ex) {
            }
        }
        System.out.println("one " + a.getCount());
    }
}

class ThreadTwo implements Runnable {
    Accum a = Accum.getAccum();

    public void run() {
        for (int x = 0; x < 99; x++) {
            a.updateCounter(1);
            try {
                Thread.sleep(50);
            } catch (InterruptedException ex) {
            }
            if (x == 97) {
                try {
                    Thread.sleep(50);
                } catch (InterruptedException ex) {
                }
            }
        }
        System.out.println("two " + a.getCount());
    }
}
不念旧人 2022-09-13 09:27:23

利用并发辅助类试一试。

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文