Java实现多线程轮流打印1-100的数字

首先打印1-100数字如果用一个单线程实现那么只要一个for循环即可,那么如果要用两个线程打印出来呢?(一个线程打印奇数,一个线程打印偶数)于是大家会想到可以通过加锁实现,但是这样的效率是不是不高?这里我用一个变量来控制两个线程的输出


public class ThreadTest {
	volatile int flag=0;
	public void runThread() throws InterruptedException{
 	   Thread t1=new Thread(new Thread1());
 	   Thread t2=new Thread(new Thread2());
 	   t1.start();
 	   t2.start();
	}
	public class Thread1 implements Runnable{

		public void run() {
			int i=0;
			while(i<=99){
				if(flag==0)
				{
					System.out.println("t1="+i+"flag="+flag);
					i+=2;
					flag=1;
				}
			}
		}
		
	}
	
	public class Thread2 implements Runnable{

		public void run() {
			int i=1;
			while(i<=99){
				if(flag==1)
				{
					System.out.println("t2="+i+"flag="+flag);
					i+=2;
					flag=0;
				}
			}
		}
		
	}
}

那么如果要实现三个线程轮流打印1-100的数字呢?是不是也可以用上面的方法实现呢?代码如下


public class ThreadTest {
	private int i=0;
	private Thread thread1,thread2,thread3;
	private int flag=0;
	public void runThread() throws InterruptedException{
 	   thread1=new Thread(new Thread1());
 	   thread2=new Thread(new Thread2());
 	   thread3=new Thread(new Thread3());
 	   thread1.start();
 	   thread2.start();
 	   thread3.start();
	}
	public class Thread1 implements Runnable{

		public void run() {
			
			while(i<=100){
				if(flag==0) {
					System.out.println("t1="+i);
					i++;
					flag=1;
				}
			}
		}
		
	}
	
	public class Thread2 implements Runnable{

		public void run() {
			
			while(i<=100){
				if(flag==1) {
					System.out.println("t2="+i);
					i++;
					flag=2;
				}
			}
		}
		
	}
	
	public class Thread3 implements Runnable{

		public void run() {
			
			while(i<=100){
				if(flag==2) {
					System.out.println("t3="+i);
					i++;
					flag=0;
				}
			}
		}
		
	}
}

运行结果

发现三个线程只打印了一次就停止不输出了,是什么原因呢?

可以用jdk自带的jstack来看看线程的状态,在windows系统中可以打开cmd然后进入jdk所在目录,然后执行Jsp,能查看到各线程id,然后执行jstack -F pid就可以看的状态了

可以看到几个Thread state是BLOCKED,就是阻塞了,什么原因呢?

尴尬发现flag变量和i变量前面忘记加volatile,导致flag和i被线程读取修改时,其他线程不可见,所以才导致上面的问题出现。

在JVM中每个线程读取变量到cache中时相互都是不可见的,也就是java五大内存区中的程序计数器区域对于每个线程都是独立的不共享的,只有堆内存区和方法区是对所有线程都是共享的。当线程1读取了flag和i的值,并对其进行修改的时候,线程2并发运行,并不知道flag和i值已经改变,导致多线程数据不一致的情况,所以加了volatile后,当线程读取变量进行修改后会“通知”其它线程这个值已经进行了修改。

import java.util.concurrent.atomic.AtomicInteger;


public class ThreadTest {
	private volatile int i=0;
	private Thread thread1,thread2,thread3;
	private volatile int flag=0;
	public void runThread() throws InterruptedException{
 	   thread1=new Thread(new Thread1());
 	   thread2=new Thread(new Thread2());
 	   thread3=new Thread(new Thread3());
 	   thread1.start();
 	   thread2.start();
 	   thread3.start();
	}
	public class Thread1 implements Runnable{

		public void run() {
			while(i<100){
				if(flag==0) {
					System.out.println("t1="+i);
					i++;
					flag=1;
				}
			}
		}
		
	}
	
	public class Thread2 implements Runnable{

		public void run() {
			
			while(i<100){
				if(flag==1){
					System.out.println("t2="+i);
					i++;
					flag=2;
				}
			}
		}
		
	}
	
	public class Thread3 implements Runnable{

		public void run() {
			
			while(i<100){
				if(flag==2){
					System.out.println("t3="+i);
					i++;
					flag=0;
				}
			}
		}
		
	}
}

运行结果

 

-----未完-----

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章