java多线程问题集锦(二)_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > java多线程问题集锦(二)

java多线程问题集锦(二)

 2011/10/10 8:06:09  jsczxy2  http://jsczxy2.iteye.com  我要评论(0)
  • 摘要:用多线程的方式实现对一个数随机自增N次,随机自减M次,最后打印出这个数字(不能用原子操作类AtomicInteger)。packagecom.test;publicclassTestThreadTwo{publicstaticvoidmain(String[]args){intnum=0;inttotal=0;intincrease_count=4000;intdecrease_count=3000;intsum_count=increase_count+decrease_count
  • 标签:多线程 Java 问题 线程

用多线程的方式实现对一个数随机自增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();
	}
}
发表评论
用户名: 匿名