java多线程问题集锦(二)

本文探讨了如何使用多线程实现对一个数的随机增减操作,并通过同步机制确保线程安全,最终打印出计算结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

用多线程的方式实现对一个数随机自增N次,随机自减M次,最后打印出这个数字(不能用原子操作类AtomicInteger)。

 

 

package com.test;

public class TestThreadTwo {

	public static void main(String[] args) {
		int num = 0;
		int total = 0;
		int increase_count = 4000;
		int decrease_count = 3000;
		int sum_count = increase_count + decrease_count;
		OperatorTwo o = new OperatorTwo(num, total);
		//打印监控线程启动
		new PrintThread(o, sum_count).start();
		//所有加减线程启动
		for(int i =0;i<increase_count;i++){
			new IncreaseThreadTwo(o).start();
		}
		for(int i =0;i<decrease_count;i++){
			new DecreaseThreadTwo(o).start();
		}
	}
}

class OperatorTwo {
	private int num;
	private int total;

	public OperatorTwo(int num, int total) {
		this.num = num;
	}

	public int getNum() {
		return num;
	}

	public int getTotal() {
		return total;
	}

	public synchronized void increase() {
		num++;
	}

	public synchronized void decrease() {
		num--;
	}

	public synchronized void increaseTotal() {
		total++;
	}
}

class IncreaseThreadTwo extends Thread {
	private OperatorTwo o;

	public IncreaseThreadTwo(OperatorTwo o) {
		this.o = o;
	}

	@Override
	public void run() {
		try {
			Thread.sleep((long) (Math.random() * 100));
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		o.increase();
		o.increaseTotal();
	}
}

class DecreaseThreadTwo extends Thread {
	private OperatorTwo o;

	public DecreaseThreadTwo(OperatorTwo o) {
		this.o = o;
	}

	@Override
	public void run() {
		try {
			Thread.sleep((long) (Math.random() * 100));
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		o.decrease();
		o.increaseTotal();
	}
}

class PrintThread extends Thread{
	private OperatorTwo o;
	private int sum_count;
	
	public PrintThread(OperatorTwo o,int sum_count) {
		this.o = o;
		this.sum_count=sum_count;
	}
	@Override
	public void run() {
		while(true){
			if(o.getTotal()==sum_count){
				System.out.println("最终结果:"+o.getNum());
				break;
			}
		}
	}
}
 

 

package com.test;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.ListIterator;

public class TestThreadOne {

	public static void main(String[] args) {
		int num = 0;
		int increase_count = 40000;
		int decrease_count = 30000;
		Operator o = new Operator(num);
		List<Thread> threads = new ArrayList<Thread>(); 
		for (int i = 0; i < increase_count; i++) {
			Thread t = new IncreaseThreadO(o);
			threads.add(t);
			t.start();
		}
		for (int i = 0; i < decrease_count; i++) {
			Thread t = new DecreaseThreadO(o);
			threads.add(t);
			t.start();
		}
		/*
		List<Thread> ts = new ArrayList<Thread>();
		while(threads.size()>0){
			for(Thread t : threads){
				if(t.isAlive()){
					ts.add(t);
				}
			}
			threads.clear();
			threads.addAll(ts);
			ts.clear();
		}*/
		int sum = increase_count + decrease_count;
		int count = 0;
		ListIterator<Thread> it = null;
		while(count<sum){
			it = threads.listIterator();
			while(it.hasNext()){
				Thread t =it.next();
				if(t!=null&&!t.isAlive()){
					count++;
					it.set(null);
				}
			}
		}
		System.out.println("最终结果"+o.getNum());
	}
}

class Operator {
	private int num;

	public Operator(int num) {
		this.num = num;
	}

	public int getNum() {
		return num;
	}

	public synchronized void increase() {
		num++;
	}

	public synchronized void decrease() {
		num--;
	}
}

class IncreaseThreadO extends Thread {
	private Operator o;

	public IncreaseThreadO(Operator o) {
		this.o = o;
	}

	@Override
	public void run() {
		try {
			Thread.sleep((long)(Math.random()*100));
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		o.increase();
	}
}

class DecreaseThreadO extends Thread {
	private Operator o;

	public DecreaseThreadO(Operator o) {
		this.o = o;
	}

	@Override
	public void run() {
		try {
			Thread.sleep((long)(Math.random()*100));
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		o.decrease();
	}
}
 

 

package com.test;

import java.util.ArrayList;
import java.util.List;

public class TestThreadOne {

	public static void main(String[] args) {
		int num = 0;
		int increase_count = 4000;
		int decrease_count = 3000;
		Operator o = new Operator(num);
		List<Thread> threads = new ArrayList<Thread>(); 
		for (int i = 0; i < increase_count; i++) {
			Thread t = new IncreaseThreadO(o);
			threads.add(t);
			t.start();
		}
		for (int i = 0; i < decrease_count; i++) {
			Thread t = new DecreaseThreadO(o);
			threads.add(t);
			t.start();
		}
		List<Thread> ts = new ArrayList<Thread>();
		while(threads.size()>0){
			for(Thread t : threads){
				if(t.isAlive()){
					ts.add(t);
				}
			}
			threads.clear();
			threads.addAll(ts);
			ts.clear();
		}
		System.out.println("最终结果"+o.getNum());
	}
}

class Operator {
	private int num;

	public Operator(int num) {
		this.num = num;
	}

	public int getNum() {
		return num;
	}

	public synchronized void increase() {
		num++;
	}

	public synchronized void decrease() {
		num--;
	}
}

class IncreaseThreadO extends Thread {
	private Operator o;

	public IncreaseThreadO(Operator o) {
		this.o = o;
	}

	@Override
	public void run() {
		try {
			Thread.sleep((long)(Math.random()*100));
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		o.increase();
	}
}

class DecreaseThreadO extends Thread {
	private Operator o;

	public DecreaseThreadO(Operator o) {
		this.o = o;
	}

	@Override
	public void run() {
		try {
			Thread.sleep((long)(Math.random()*100));
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		o.decrease();
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值