• java常见并发面试题目+示例代码


    1、现在有线程 T1、T2 和 T3。你如何确保 T2 线程在 T1 之后执行,并且 T3 线程在 T2 之后执行?

    这个线程面试题通常在第一轮面试或电话面试时被问到,这道多线程问题为了测试面试者是否熟悉join方法的概念。答案也非常简单——可以用 Thread 类的join方法实现这一效果。

     1 public class Test {
     2 
     3     public static void main(String[] args) throws InterruptedException {
     4         Thread t1 = new Thread() {
     5             public void run() {
     6                 System.out.println("1");
     7             }
     8         };
     9         Thread t2 = new Thread() {
    10             public void run() {
    11                 System.out.println("2");
    12             }
    13         };
    14         Thread t3 = new Thread() {
    15             public void run() {
    16                 System.out.println("3");
    17             }
    18         };
    19         
    20         t1.start();
    21         t1.join();//阻塞住直到t1完成
    22         t2.start();
    23         t2.join();
    24         t3.start();
    25         System.out.println("end");
    26     }
    27 }
    View Code

     2、两线程奇偶数打印

     1 public class Test {
     2     static class SoulutionTask implements Runnable {
     3         static int value = 0;
     4 
     5         @Override
     6         public void run() {
     7             while (value <= 100) {
     8                 synchronized (SoulutionTask.class) {
     9                     System.out.println(Thread.currentThread().getName() + ":" + value++);
    10                     SoulutionTask.class.notify();
    11                     try {
    12                         SoulutionTask.class.wait();
    13                     } catch (InterruptedException e) {
    14                         e.printStackTrace();
    15                     }
    16                 }
    17             }
    18         }
    19     }
    20 
    21     public static void main(String[] args) throws InterruptedException {
    22         new Thread(new SoulutionTask(), "偶数").start();
    23         new Thread(new SoulutionTask(), "奇数").start();
    24     }
    25 }
    View Code

    N个线程循环打印

     1 public class Test implements Runnable {
     2     private static final Object LOCK = new Object();
     3     /**
     4      * 当前即将打印的数字
     5      */
     6     private static int current = 0;
     7     /**
     8      * 当前线程编号,从0开始
     9      */
    10     private int threadNo;
    11     /**
    12      * 线程数量
    13      */
    14     private int threadCount;
    15     /**
    16      * 打印的最大数值
    17      */
    18     private int maxInt;
    19 
    20     public Test(int threadNo, int threadCount, int maxInt) {
    21         this.threadNo = threadNo;
    22         this.threadCount = threadCount;
    23         this.maxInt = maxInt;
    24     }
    25 
    26     @Override
    27     public void run() {
    28         while (true) {
    29             synchronized (LOCK) {
    30                 // 判断是否轮到当前线程执行
    31                 while (current % threadCount != threadNo) {
    32                     if (current > maxInt) {
    33                         break;
    34                     }
    35                     try {
    36                         // 如果不是,则当前线程进入wait
    37                         LOCK.wait();
    38                     } catch (Exception e) {
    39                         e.printStackTrace();
    40                     }
    41                 }
    42                 // 最大值跳出循环
    43                 if (current > maxInt) {
    44                     break;
    45                 }
    46                 System.out.println("thread" + threadNo + " : " + current);
    47                 current++;
    48                 // 唤醒其他wait线程
    49                 LOCK.notifyAll();
    50             }
    51         }
    52     }
    53 
    54     public static void main(String[] args) {
    55         int threadCount = 3;
    56         int max = 100;
    57         for (int i = 0; i < threadCount; i++) {
    58             new Thread(new Test(i, threadCount, max)).start();
    59         }
    60     }
    61 }
    View Code
  • 相关阅读:
    超级迷宫我的计划表
    不敢死队
    Let the Balloon Rise
    Hangover
    汉诺塔系列2
    Tri Tiling(递推)
    Tiling(递推,高精度)
    Color Me Less
    I Think I Need a Houseboat(圆计算)
    Kbased Numbers(递推)
  • 原文地址:https://www.cnblogs.com/shaozhen/p/11214653.html
Copyright © 2020-2023  润新知