为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

慕田峪9158850 2019-02-21 01:03:20
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
查看完整描述

3 回答

?
手掌心

TA贡献1942条经验 获得超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());

    }
}
查看完整回答
反对 回复 2019-03-01
?
噜噜哒

TA贡献1784条经验 获得超7个赞

参考楼上,给出我的两种实现。看起来没那么高大上,但是好理解一些。
实现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());
    }
}
查看完整回答
反对 回复 2019-03-01
?
森栏

TA贡献1810条经验 获得超5个赞

利用并发辅助类试一试。

查看完整回答
反对 回复 2019-03-01
  • 3 回答
  • 0 关注
  • 445 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信