Java线程交替打印数字

Java线程:三个线程交替打印1-75,每隔5个数交替一次


解题思想:

  1. 使用lock锁和condition,来使线程交替打印;
  2. 每隔5个数释放一次锁;
  3. 需要一个共同的index,控制打印的数字;
  4. 需要一个变量控制每隔5个数释放一次锁。

   代码实现如下所示:

/**
 * 
 * @author luoluo
 *  三个线程交替打印数组
 */
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
 
public class Print1_75 {
    public static int i = 1;
    public static void main(String[] args) {
        //创建锁对象
        Lock lock = new ReentrantLock();
        Condition condition1 = lock.newCondition();
        Condition condition2 = lock.newCondition();
        Condition condition3 = lock.newCondition();
 
        //创建线程池
        ExecutorService threadPool = Executors.newFixedThreadPool(3);
        //提交线程
        threadPool.submit(new Runnable() {
            @Override
            public void run() {
            	int j=1;
                while (true) {
                    lock.lock();
                    try {
                        //线程 1 休眠
                        condition1.await();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    while(i <=75) {
                        System.out.println(Thread.currentThread().getName() + ":" + i);
                        i++;
                        j++;
                        if (j>5) {
							j=1;
							break;
							
						}
                    }
                    //唤醒 2 线程
                    condition2.signal();
                    lock.unlock();
                }
            }
        });
 
        threadPool.submit(new Runnable() {
            @Override
            public void run() {
            	int j=1;
                while (true) {
                	
                    lock.lock();
                    try {
                        // 线程 2 休眠
                        condition2.await();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    while(i <=75) {
                        System.out.println(Thread.currentThread().getName() + ":" + i);
                        i++;
                        j++;
                        if (j>5) {
                        	j=1;
							break;
						}
                    }
                    //唤醒 3 线程
                    condition3.signal();
                    lock.unlock();
                }
            }
        });
 
        threadPool.submit(new Runnable() {
            @Override
            public void run() {
            	int j=1;
                while (true) {
                    lock.lock();
                    try {
                        //线程 3 休眠
                        condition3.await();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    while (i <=75) {
                        System.out.println(Thread.currentThread().getName() + ":" + i);
                        i++;
                        j++;
                        if (j>5) {
							j=1;
							break;
						}
                    }
                    //唤醒 1 线程
                    condition1.signal();
                    lock.unlock();
                }
            }
        });
 
        //专用唤醒 1 线程
        new Thread() {
            @Override
            public void run() {
                lock.lock();
                condition1.signal();
                lock.unlock();
            }
        }.start();
 
    }

代码结果如下所示(为了方便现实只打印到30):

pool-1-thread-1:1
pool-1-thread-1:2
pool-1-thread-1:3
pool-1-thread-1:4
pool-1-thread-1:5
pool-1-thread-2:6
pool-1-thread-2:7
pool-1-thread-2:8
pool-1-thread-2:9
pool-1-thread-2:10
pool-1-thread-3:11
pool-1-thread-3:12
pool-1-thread-3:13
pool-1-thread-3:14
pool-1-thread-3:15
pool-1-thread-1:16
pool-1-thread-1:17
pool-1-thread-1:18
pool-1-thread-1:19
pool-1-thread-1:20
pool-1-thread-2:21
pool-1-thread-2:22
pool-1-thread-2:23
pool-1-thread-2:24
pool-1-thread-2:25
pool-1-thread-3:26
pool-1-thread-3:27
pool-1-thread-3:28
pool-1-thread-3:29
pool-1-thread-3:30

 

你可能感兴趣的:(Java线程)