多线程经典笔试题集锦

Posted Java码农社区

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了多线程经典笔试题集锦相关的知识,希望对你有一定的参考价值。

多线程经典笔试题集锦

题一

1、需求

要求用三个线程,线程1先打印1,2,3,4,5, 然后是线程2打印6,7,8,9,10, 然后是线程3打印11,12,13,14,15. 接着再由线程1打印16,17,18,19,20....以此类推,直到输出到45。

2、实现

package com.ctw.t1;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
* @author chentongwei@baidu-mgame.com 2018-11-22 16:51:26
* @Desc 要求用三个线程,线程1先打印1,2,3,4,5, 然后是线程2打印6,7,8,9,10, 然后是线程3打印11,12,13,14,15. 接着再由线程1打印16,17,18,19,20....以此类推,直到输出到45
*/
public class Demo {

   public static void main(String[] args) {
       Object lockObj = new Object();
       ExecutorService threadPool = Executors.newFixedThreadPool(3);

       threadPool.execute(new MyRunnable(0, lockObj));
       threadPool.execute(new MyRunnable(1, lockObj));
       threadPool.execute(new MyRunnable(2, lockObj));
       threadPool.shutdown();
   }
}

class MyRunnable implements Runnable {

   private static int num = 1;

   private int threadId;

   private Object lockObj;

   public MyRunnable(int threadId, Object lockObj) {
       this.threadId = threadId;
       this.lockObj = lockObj;
   }

   @Override
   public void run() {
       synchronized (lockObj) {
           while (num <= 45) {
               if (num / 5 % 3 == threadId) {
                   for (int i = 0; i < 5; i++) {
                       System.out.println("ThreadName: " + Thread.currentThread().getName() + ", num is: " + num ++);
                   }
                   lockObj.notifyAll();
               } else {
                   try {
                       lockObj.wait();
                   } catch (InterruptedException e) {
                       e.printStackTrace();
                   }
               }
           }
       }
   }
}

3、结果

ThreadName: pool-1-thread-1, num is: 1
ThreadName: pool-1-thread-1, num is: 2
ThreadName: pool-1-thread-1, num is: 3
ThreadName: pool-1-thread-1, num is: 4
ThreadName: pool-1-thread-1, num is: 5
ThreadName: pool-1-thread-2, num is: 6
ThreadName: pool-1-thread-2, num is: 7
ThreadName: pool-1-thread-2, num is: 8
ThreadName: pool-1-thread-2, num is: 9
ThreadName: pool-1-thread-2, num is: 10
ThreadName: pool-1-thread-3, num is: 11
ThreadName: pool-1-thread-3, num is: 12
ThreadName: pool-1-thread-3, num is: 13
ThreadName: pool-1-thread-3, num is: 14
ThreadName: pool-1-thread-3, num is: 15
ThreadName: pool-1-thread-1, num is: 16
ThreadName: pool-1-thread-1, num is: 17
ThreadName: pool-1-thread-1, num is: 18
ThreadName: pool-1-thread-1, num is: 19
ThreadName: pool-1-thread-1, num is: 20
ThreadName: pool-1-thread-2, num is: 21
ThreadName: pool-1-thread-2, num is: 22
ThreadName: pool-1-thread-2, num is: 23
ThreadName: pool-1-thread-2, num is: 24
ThreadName: pool-1-thread-2, num is: 25
ThreadName: pool-1-thread-3, num is: 26
ThreadName: pool-1-thread-3, num is: 27
ThreadName: pool-1-thread-3, num is: 28
ThreadName: pool-1-thread-3, num is: 29
ThreadName: pool-1-thread-3, num is: 30
ThreadName: pool-1-thread-1, num is: 31
ThreadName: pool-1-thread-1, num is: 32
ThreadName: pool-1-thread-1, num is: 33
ThreadName: pool-1-thread-1, num is: 34
ThreadName: pool-1-thread-1, num is: 35
ThreadName: pool-1-thread-2, num is: 36
ThreadName: pool-1-thread-2, num is: 37
ThreadName: pool-1-thread-2, num is: 38
ThreadName: pool-1-thread-2, num is: 39
ThreadName: pool-1-thread-2, num is: 40
ThreadName: pool-1-thread-3, num is: 41
ThreadName: pool-1-thread-3, num is: 42
ThreadName: pool-1-thread-3, num is: 43
ThreadName: pool-1-thread-3, num is: 44
ThreadName: pool-1-thread-3, num is: 45

题二

1、需求

要求用三个线程,线程1先打印A,然后是线程2打印B, 然后是线程3打印C. 接着再由线程1打印A....以此类推,打印10次ABC

2、实现

package com.ctw.t1;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
* @author chentongwei@baidu-mgame.com 2018-11-22 17:34:58
* @Desc 要求用三个线程,线程1先打印A,然后是线程2打印B, 然后是线程3打印C. 接着再由线程1打印A....以此类推,打印10次ABC
*/
public class Demo1 {

   public static void main(String[] args) {
       Lock lock = new ReentrantLock();
       ExecutorService threadPool = Executors.newFixedThreadPool(3);

       threadPool.execute(new MyRunnable1("A", lock, 0));
       threadPool.execute(new MyRunnable1("B", lock, 1));
       threadPool.execute(new MyRunnable1("C", lock, 2));
       threadPool.shutdown();
   }
}

class MyRunnable1 implements Runnable {

   private String str;

   private Lock lock;

   private int threadId;

   private static int count = 0;

   private static final int MAX = 30;

   public MyRunnable1(String str, Lock lock, int threadId) {
       this.str = str;
       this.lock = lock;
       this.threadId = threadId;
   }

   @Override
   public void run() {
       while (true) {
           lock.lock();
           // 为什么在这加判断,而不是在while里面写count<=MAX,因为那样的话会打印11次。
           if (count >= MAX) {
               lock.unlock();
               return;
           }
           if (count % 3 == threadId) {
               System.out.println(str);
               count ++ ;
           }
           lock.unlock();
       }
   }
}

3、结果

A
B
C
A
B
C
A
B
C
A
B
C
A
B
C
A
B
C
A
B
C
A
B
C
A
B
C
A
B
C

题三

1、需求

现有三组字符串:ABC/DEF/GHI,要求用线程打印出A D G B E H C F I

2、实现

package com.ctw.t1;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
* @author chentongwei@baidu-mgame.com 2018-11-22 17:34:58
* @Desc: 现有三组字符串:ABC/DEF/GHI,要求用线程打印出A D G B E H C F I
*/
public class Demo2 {

   public static void main(String[] args) {
       Lock lock = new ReentrantLock();
       ExecutorService threadPool = Executors.newFixedThreadPool(3);

       threadPool.execute(new MyRunnable2("ABC", lock, 0));
       threadPool.execute(new MyRunnable2("DEF", lock, 1));
       threadPool.execute(new MyRunnable2("GHI", lock, 2));
       threadPool.shutdown();
   }
}

class MyRunnable2 implements Runnable {

   private String str;

   private Lock lock;

   private int threadId;

   private static int count = 0;

   private static final int MAX = 9;

   public MyRunnable2(String str, Lock lock, int threadId) {
       this.str = str;
       this.lock = lock;
       this.threadId = threadId;
   }

   @Override
   public void run() {
       while (true) {
           lock.lock();
           // 为什么在这加判断,而不是在while里面写count<=MAX,因为那样的话会打印11次。
           if (count >= MAX) {
               lock.unlock();
               return;
           }
           if (count % 3 == threadId) {
               System.out.print(str.charAt(count / 3) + " ");
               count ++ ;
           }
           lock.unlock();
       }
   }
}

3、结果

A D G B E H C F I 

广告

  • https://gitee.com/geekerdream/




以上是关于多线程经典笔试题集锦的主要内容,如果未能解决你的问题,请参考以下文章

经典笔试题:线程通信(使用CountDownLatch实现线程间通信)

经典笔试题:两个线程交替打印奇偶数

经典笔试题:线程通信(使用wait,notify实现线程间通信)

经典笔试题:线程通信(使用重入锁(ReentrantLock)和条件队列(Condition)实现线程间通信)

多线程关于腾讯笔试题

经典PHP笔试题