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());
}
}
噜噜哒
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());
}
}
添加回答
举报
0/150
提交
取消