java多线程交替输出

JerezNx 2017-10-20 04:43:47
新人初学线程,想实现线程1输出1 2 3 4 5,然后线程2输出6, 7 8 9 10,再是线程1输出11 12 13 14 15,这样一直输出到50.

代码如下:


package 多线程;

public class 线程同步2 {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub

TongBu1 t = new TongBu1();
Thread p1 = new Thread(t);
p1.setName("线程1");
p1.start();

Thread p2 = new Thread(t);
p2.setName("线程2");
p2.start();
}

}


class TongBu1 implements Runnable{

static int count =1;
public synchronized void run() {
for (int i = count; i <= 50; i++) {
try {
System.out.println(Thread.currentThread().getName()+"输入:"+i);
if (i-count==4) {
count=i+1;
notify();
wait();
}

} catch (Exception e) {
// TODO: handle exception
}
}
}
}

==========================================================================
结果输出为:
线程1输入:1
线程1输入:2
线程1输入:3
线程1输入:4
线程1输入:5
线程2输入:6
线程2输入:7
线程2输入:8
线程2输入:9
线程2输入:10
线程1输入:6
线程1输入:7
线程1输入:8
线程1输入:9
线程1输入:10
线程1输入:11
线程1输入:12
线程1输入:13
线程1输入:14
线程1输入:15
线程2输入:11
线程2输入:12
线程2输入:13
线程2输入:14
线程2输入:15
线程2输入:16
线程2输入:17
线程2输入:18
线程2输入:19
线程2输入:20
线程1输入:16
线程1输入:17
线程1输入:18
线程1输入:19
线程1输入:20
线程1输入:21
线程1输入:22
线程1输入:23
线程1输入:24
线程1输入:25
线程2输入:21
线程2输入:22
线程2输入:23
线程2输入:24
线程2输入:25
线程2输入:26
线程2输入:27
线程2输入:28
线程2输入:29
线程2输入:30
线程1输入:26
线程1输入:27
线程1输入:28
线程1输入:29
线程1输入:30
线程1输入:31
线程1输入:32
线程1输入:33
线程1输入:34
线程1输入:35
线程2输入:31
线程2输入:32
线程2输入:33
线程2输入:34
线程2输入:35
线程2输入:36
线程2输入:37
线程2输入:38
线程2输入:39
线程2输入:40
线程1输入:36
线程1输入:37
线程1输入:38
线程1输入:39
线程1输入:40
线程1输入:41
线程1输入:42
线程1输入:43
线程1输入:44
线程1输入:45
线程2输入:41
线程2输入:42
线程2输入:43
线程2输入:44
线程2输入:45
线程2输入:46
线程2输入:47
线程2输入:48
线程2输入:49
线程2输入:50
线程1输入:46
线程1输入:47
线程1输入:48
线程1输入:49
线程1输入:50
================================================================================

调试过程中,发现


count明明是11,为什么i被赋值为6?
...全文
473 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
qq_39912309 2017-10-21
  • 打赏
  • 举报
回复
引用 3 楼 u014034934 的回复:
[quote=引用 2 楼 qq_39912309 的回复:] [quote=引用 1 楼 u014034934 的回复:] 帮你改了一下

	static int count = 0;

	public synchronized void run() {
		while (count++ <= 49) {
			try {
				System.out.println(Thread.currentThread().getName() + "输出: " + count);
				if (count % 5 == 0) {
					notify();
					wait();
				}
			} catch (Exception e) {}
		}
		notify();
	}
请问为什么最后要有个notify()啊?[/quote] 因为完成任务后,另一个线程是wait状态,要notify()[/quote]notify()唤醒后线程会自动结束吗?不唤醒又有什么不妥呢?
逗比123号 2017-10-21
  • 打赏
  • 举报
回复
引用 4 楼 JerezNx 的回复:
[quote=引用 1 楼 u014034934 的回复:] 帮你改了一下

	static int count = 0;

	public synchronized void run() {
		while (count++ <= 49) {
			try {
				System.out.println(Thread.currentThread().getName() + "输出: " + count);
				if (count % 5 == 0) {
					notify();
					wait();
				}
			} catch (Exception e) {}
		}
		notify();
	}
你好,你的这种写法我刚才试了,没问题。 但我想弄明白为什么我那样用for写会出错,我调试的时候: p2先运行,然后p1,到这里还没问题,但接下来p2运行的时候, 明明 count的值是11,但i却被赋值为6了,这里弄不明白。。[/quote] 因为你for循环里count只赋值一次,你输出的是i,两个线程有各自的i,所以会输出重复的数字。所以我就把i给去掉了,直接输出count。
JerezNx 2017-10-20
  • 打赏
  • 举报
回复
引用 1 楼 u014034934 的回复:
帮你改了一下

	static int count = 0;

	public synchronized void run() {
		while (count++ <= 49) {
			try {
				System.out.println(Thread.currentThread().getName() + "输出: " + count);
				if (count % 5 == 0) {
					notify();
					wait();
				}
			} catch (Exception e) {}
		}
		notify();
	}
你好,你的这种写法我刚才试了,没问题。 但我想弄明白为什么我那样用for写会出错,我调试的时候: p2先运行,然后p1,到这里还没问题,但接下来p2运行的时候, 明明 count的值是11,但i却被赋值为6了,这里弄不明白。。
逗比123号 2017-10-20
  • 打赏
  • 举报
回复
引用 2 楼 qq_39912309 的回复:
[quote=引用 1 楼 u014034934 的回复:] 帮你改了一下

	static int count = 0;

	public synchronized void run() {
		while (count++ <= 49) {
			try {
				System.out.println(Thread.currentThread().getName() + "输出: " + count);
				if (count % 5 == 0) {
					notify();
					wait();
				}
			} catch (Exception e) {}
		}
		notify();
	}
请问为什么最后要有个notify()啊?[/quote] 因为完成任务后,另一个线程是wait状态,要notify()
qq_39912309 2017-10-20
  • 打赏
  • 举报
回复
引用 1 楼 u014034934 的回复:
帮你改了一下

	static int count = 0;

	public synchronized void run() {
		while (count++ <= 49) {
			try {
				System.out.println(Thread.currentThread().getName() + "输出: " + count);
				if (count % 5 == 0) {
					notify();
					wait();
				}
			} catch (Exception e) {}
		}
		notify();
	}
请问为什么最后要有个notify()啊?
逗比123号 2017-10-20
  • 打赏
  • 举报
回复
帮你改了一下

	static int count = 0;

	public synchronized void run() {
		while (count++ <= 49) {
			try {
				System.out.println(Thread.currentThread().getName() + "输出: " + count);
				if (count % 5 == 0) {
					notify();
					wait();
				}
			} catch (Exception e) {}
		}
		notify();
	}

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧