Java 每日算法,三个线程按顺序打印

原文链接: https://blog.csdn.net/u010539469/article/details/77803259

关于多线程的基础知识,可点击下面链接进行学习。

JAVA\Android 多线程实现方式及并发与同步

 

题目1

启动3个线程A、B、C,使A打印0,然后B打印1,然后C打印2,A打印3,B打印4,C打印5,依次类推。

public class PrintSequenceThread implements Runnable {
    private static final Object LOCK = new Object();
    /**
     * 当前即将打印的数字
     */
    private static int current = 0;
 
    /**
     * 当前线程编号,从0开始
     */
    private int threadNo;
 
    /**
     * 线程数量
     */
    private int threadCount;
 
    /**
     * 打印的最大数值
     */
    private int max;
 
    public PrintSequenceThread(int threadNo, int threadCount, int max) {
        this.threadNo = threadNo;
        this.threadCount = threadCount;
        this.max = max;
    }
 
    @Override
    public void run() {
        while(true) {
            synchronized (LOCK) {
                // 判断是否轮到当前线程执行
                while (current % threadCount != threadNo) {
                    if (current > max) {
                        break;
                    }
                    try {
                        // 如果不是,则当前线程进入wait
                        LOCK.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                // 最大值跳出循环
                if (current > max) {
                    break;
                }
                System.out.println("thread-" + threadNo + " : " + current);
                current++;
                // 唤醒其他wait线程
                LOCK.notifyAll();
            }
        }
    }
 
    public static void main(String[] args) {
        int threadCount = 3;
        int max = 10;
        for(int i=0;i

题目2

编写一个程序,开启 3 个线程,这三个线程的分别为 A、B、C,每个线程打印对应的“A”、“B”、“C” 10 遍,要求输出的结果必须按顺序显示。如:ABCABCABC…… 

*tate初始化为1,如果state为1执行A线程,A线程修改state为2执行B线程;如果state为2执行B线程,B线程修改state为3企图执行C线程;如果state为3执行C线程,C线程修改state为1,企图执行A线程。

解法1:采用volatile修饰的全局变量state作为条件控制,用for循环加if条件判断的忙等模式。

package com.yangliu.lock;
 
import java.io.File;
 
public class ABC1 {
	private static int n = 100000; //控制线程执行次数
	private volatile static int state = 0; //控制线程执行条件
	static class ThreadA extends Thread {
		public void run() {
           for(int i=0;i

问题:为什么共享变量要加volatile?加volatile就足够了吗?

分析:因为加volatile可以保证变量state的可见性,上一个线程对state的修改对下一个线程是可见的。另外由于有if条件做判断,所以可以确保只有单一的线程修改变量state的值,这里用volatile就足够了。

解法2:采用原子类AtomicInteger来控制变量state,其他不变
 

package com.yangliu.lock;
 
import java.io.File;
import java.util.concurrent.atomic.AtomicInteger;
 
public class ABC2 {
	private static int n = 100000; //控制线程执行次数
	private static AtomicInteger state = new AtomicInteger(0); //控制线程执行条件
	static class ThreadA extends Thread {
		public void run() {
           for(int i=0;i

解法3:采用ReentrantLock锁住整段代码

package com.yangliu.lock;
 
import java.io.File;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class ABC3 {
	private static int n = 100000; // 控制线程执行次数
	private static int state = 0; // 控制线程执行条件
	private static Lock lock = new ReentrantLock();
	static class ThreadA extends Thread {
		public void run() {
			for (int i = 0; i < n;) {
				lock.lock();
				if (state % 3 == 0) {
					System.out.println("A,loopNum=" + i);
					state++;
					i++;
				}
				lock.unlock();
			}
		}
	}
	static class ThreadB extends Thread {
		public void run() {
			for (int i = 0; i < n;) {
				lock.lock();
				if (state % 3 == 1) {
					System.out.println("B,loopNum=" + i);
					state++;
					i++;
				}
				lock.unlock();
			}
		}
	}
	static class ThreadC extends Thread {
		public void run() {
			for (int i = 0; i < n;) {
				lock.lock();
				if (state % 3 == 2) {
					System.out.println("C,loopNum=" + i);
					state++;
					i++;
				}
				lock.unlock();
			}
		}
	}
	public static void main(String[] args) throws InterruptedException {
		 long startTime = System.currentTimeMillis();
		 new ThreadA().start();
		 new ThreadB().start();
		 ThreadC c =new ThreadC();
	 	   c.start();
	 	   c.join();
		 long endTime = System.currentTimeMillis();
		 File f = new File("f.txt");
		 FileUtil.writeToFile(f, "RunTime is "+(double)(endTime-startTime)/1000+"s");
	}
}

解法4:采用ReentrantLock,三个Condition进行await和signal操作

public class ABC4 {
	private static int n = 100000; // 控制线程执行次数
	private static Lock lock = new ReentrantLock();
	private static Condition A = lock.newCondition();
	private static Condition B = lock.newCondition();
	private static Condition C = lock.newCondition();
	private static int state = 0;
 
	private static class ThreadA extends Thread {
		public void run() {
			lock.lock();
			try {
				for (int i = 0; i < n;i++) {
					if (state % 3 != 0)
						A.await();
					System.out.println("A,loopNum=" + i);
					state++;
					B.signal();
				}
			} catch (InterruptedException e) {
 
				e.printStackTrace();
			} finally {
				lock.unlock();
			}
 
		}
	}
	static class ThreadB extends Thread {
		public void run() {
			lock.lock();
			try {
				for (int i = 0; i < n;i++) {
					if (state % 3 != 1)
						B.await();
					System.out.println("B,loopNum=" + i);
					state++;
					C.signal();
				}
			} catch (InterruptedException e) {
 
				e.printStackTrace();
			} finally {
				lock.unlock();
			}
 
		}
	}
	static class ThreadC extends Thread {
		public void run() {
			lock.lock();
			try {
				for (int i = 0; i < n;i++) {
					if (state % 3 != 2)
						C.await();
					System.out.println("C,loopNum=" + i);
					state++;
					A.signal();
				}
			} catch (InterruptedException e) {
 
				e.printStackTrace();
			} finally {
				lock.unlock();
			}
 
		}
	}
	public static void main(String[] args) throws InterruptedException {
		 long startTime = System.currentTimeMillis();
		 new ThreadA().start();
		 new ThreadB().start();
		 ThreadC c =new ThreadC();
	 	 c.start();
	 	 c.join();
		 long endTime = System.currentTimeMillis();
		 File f = new File("f.txt");
		 FileUtil.writeToFile(f, "RunTime is "+(double)(endTime-startTime)/1000+"s");
	}
}

解法5:使用信号量机制,完全不用state变量进行条件控制

public class ABC5 {
	private static int n = 100000;
    private static Semaphore AB = new Semaphore(0);
    private static Semaphore BC = new Semaphore(0);
    private static Semaphore CA = new Semaphore(0);
    
    static class ThreadA extends Thread {
 
        @Override
        public void run() {
            try {
                for (int i = 0; i < n; i++) {
                    CA.acquire();
                    System.out.println("A,loopNum=" + i);
                    AB.release();
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        
    }
    static class ThreadB extends Thread {
 
        @Override
        public void run() {
            try {
                for (int i = 0; i < n; i++) {
                    AB.acquire();
                    System.out.println("B,loopNum=" + i);
                    BC.release();
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        
    }
    static class ThreadC extends Thread {
 
        @Override
        public void run() {
            try {
                for (int i = 0; i < n; i++) {
                    BC.acquire();
                    System.out.println("C,loopNum=" + i);
                    CA.release();
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
    public static void main(String[] args) throws InterruptedException {
       long startTime = System.currentTimeMillis();
       CA.release(); //释放CA,让A线程先执行
 	   new ThreadA().start();
 	   new ThreadB().start();
 	   ThreadC c =new ThreadC();
 	   c.start();
 	   c.join();
 	   long endTime = System.currentTimeMillis();
 	   File f = new File("f.txt");
	   FileUtil.writeToFile(f, "RunTime is "+(double)(endTime-startTime)/1000+"s");
 	  
    }
}

当n取10万时的运行时间比较:

n=100000
RunTime is 5.623s
RunTime is 5.322s
RunTime is 4.482s
RunTime is 4.279s
RunTime is 3.842s

结果可知,

第5种信号量机制运行时间最少,最佳。

 

 

你可能感兴趣的:(算法结构+Java基础)