Java-线程(十个线程实现1~100求和)

这道题我大概搞了两个小时左右吧。
可是我发现总是不对,最后发现我做不出来了,因为我并未真正搞清整个程序的执行,以及对那些关键词(我指的是synchronized、wait()、notify()等这些玩意儿)的理解和使用都不清楚,更别说写出来整个程序了。

public class Forth2 {
	public static void main(String[] args) {
		MyRunnable3 mr = new MyRunnable3();
		Thread t1 = new Thread(mr, "Thread1");
		Thread t2 = new Thread(mr, "Thread2");
		Thread t3 = new Thread(mr, "Thread3");
		Thread t4 = new Thread(mr, "Thread4");
		Thread t5 = new Thread(mr, "Thread5");
		Thread t6 = new Thread(mr, "Thread6");
		Thread t7 = new Thread(mr, "Thread7");
		Thread t8 = new Thread(mr, "Thread8");
		Thread t9 = new Thread(mr, "Thread9");
		Thread t10 = new Thread(mr, "Thread10");
		t1.start();
		t2.start();
		t3.start();
		t4.start();
		t5.start();
		t6.start();
		t7.start();
		t8.start();
		t9.start();
		t10.start();
		mr.printSum();
		
	}
}
class MyRunnable3 implements Runnable {
	static int[] sum = new int[10];
	static int[] thread = {1, 11, 21, 31, 41, 51, 61, 71, 81, 91};
	public synchronized void run() {
		while (true) {
			if (!Thread.currentThread().getName().equals("Thread1")) {
				try {
					this.wait();
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
			} else if (thread[0] <= 10)
			{
				sum[0] += thread[0]++;
				this.notifyAll();
			}
				
			if (!Thread.currentThread().getName().equals("Thread2")) {
				try {
					this.wait();
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
			} else if (thread[1] < 20)
			{
				sum[1] += thread[1]++;
				this.notifyAll();
			}
			
			if (!Thread.currentThread().getName().equals("Thread3")) {
				try {
					this.wait();
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
			} else if (thread[2] <= 30)
			{
				sum[2] += thread[2]++;	
				this.notifyAll();
			}
			
			//下面的是未修改为上面做法的时候,知道全部修改后也不对,所以放弃修改了。。。
			if (Thread.currentThread().getName() == "Thread4") {
				if (thread[3] <= 40)
					sum[3] += thread[3]++;
			}
			if (Thread.currentThread().getName() == "Thread5") {
				if (thread[4] < 50) 
					sum[1] += thread[4]++;
				
			}
			if (Thread.currentThread().getName() == "Thread6") {
				if (thread[5] <= 60) 
					sum[2] += thread[5]++;		
			}
			if (Thread.currentThread().getName() == "Thread7") {
				if (thread[6] <= 70)
					sum[0] += thread[6]++;
			}
			if (Thread.currentThread().getName() == "Thread8") {
				if (thread[7] < 80) 
					sum[1] += thread[7]++;
				
			}
			if (Thread.currentThread().getName() == "Thread9") {
				if (thread[8] <= 90) 
					sum[2] += thread[8]++;		
			}
			if (Thread.currentThread().getName() == "Thread10") {
				if (thread[9] <= 100) 
					sum[2] += thread[9]++;		
			}
		}
	}
	public void printSum() {
		System.out.println(sum[0]);
		System.out.println(sum[1]);
		System.out.println(sum[2]);
		System.out.println(sum[3]);
		System.out.println(sum[4]);
		System.out.println(sum[5]);
		System.out.println(sum[6]);
		System.out.println(sum[7]);
		System.out.println(sum[8]);
		System.out.println(sum[9]);
		System.out.println(sum[0] + sum[1] + sum[2] + sum[3] + sum[4] + sum[5] + sum[6] + sum[7] + sum[8] + sum[9]);
	}
}

看了正确答案后,有以下感想:
1、没有完全把握住封装思想,没有养成在写程序前先进行思考的习惯:
对于每一组的相加操作,应该找到规律,然后将其封装为一个方法!这样会使得程序更加简单。
2、join()的必要:通过for循环中的join()可以确保这十个线程逐个、完整的执行下去!无需考虑在此之前这些线程的执行情况,因为进入这个循环后,每个线程实际上都是重新开始执行的。
否则,程序的结果是随机的!为什么没有join()时结果会是随机的?在这种情况下使程序停下来的标准是什么?
3、试验了很多次。。好吧 我混乱了


猜你喜欢

转载自blog.csdn.net/jy_z11121/article/details/84490436