猿问

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

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
慕田峪9158850
浏览 446回答 3
3回答

手掌心

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()); } }

噜噜哒

参考楼上,给出我的两种实现。看起来没那么高大上,但是好理解一些。实现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()); } }

森栏

利用并发辅助类试一试。
随时随地看视频慕课网APP

相关分类

Java
我要回答