转载

你真得会写java多线程手撕代码题吗?

你真得会写java多线程手撕代码题吗?

多线程部分是java中最重要的一部分之一,所以也是面试官经常问的部分。

头条面试的时候,经常会让你手撕一些多线程代码题。下面介绍几个经典

得面试题

两个线程,线程1打印A,线程2打印B,两个线程同时并发,要求保证先打印A,后打印B

使用synchronized+ wait、notify

public class Main{
    static class Number{
        public int num = 1;
        Number(){}
    }
    public static  Number number = new Number();
    public static void main(String []args) {
        Thread a = new Thread(new Runnable(){
            @Override
            public void run() {
                synchronized(number){
                    try{
                    	//避免假唤醒
                        while(number.num == 2) number.wait();
                    }catch(Exception e) {
                        e.printStackTrace();
                    }
                    System.out.println("A");
                    number.num = 2;
                    number.notify();
                }
            }
        });

        Thread b = new Thread(new Runnable(){
            @Override
            public void run() {
                synchronized(number) {
                    try{
                        while(number.num == 1) number.wait();
                    }catch(Exception e){
                        e.printStackTrace();
                    }
                    System.out.println("B");
                    number.num = 1;
                    number.notify();
                }
            }
        });
        b.start();
        try{
            Thread.sleep(1000);
        }catch(Exception e) {
            e.printStackTrace();
        }
        a.start();
    }

}

使用ReetrantLock+Condition

import java.util.concurrent.locks.*;
public class Main{
    public static  ReentrantLock lock = new ReentrantLock();
    public static Condition condition = lock.newCondition();
    public static int num = 1;
    public static void main(String []args) {
        Thread a = new Thread(new Runnable(){
            @Override
            public void run() {
                lock.lock();
                try{
                    while(num == 2) condition.await();
                }catch(Exception e) {
                    e.printStackTrace();
                }
                System.out.println("A");
                num = 2;
                condition.signal();
                lock.unlock();
            }
        });

        Thread b = new Thread(new Runnable(){
            @Override
            public void run() {
                lock.lock();
                try{
                    while(num == 1) condition.await();
                }catch(Exception e) {
                    e.printStackTrace();
                }
                System.out.println("B");
                num = 1;
                condition.signal();
                lock.unlock();
            }
        });
        b.start(); 
        try{
            Thread.sleep(1000);
        }catch(Exception e){
            e.printStackTrace();
        };
        a.start();
    }

}

两个线程,一个线程打印奇数,一个线程打印偶数,控制先一个奇数后一个偶数这种顺序.使用ReetrantLock+Condition

import java.util.concurrent.locks.*;
public class Main{
    public static  ReentrantLock lock = new ReentrantLock();
    public static Condition condition = lock.newCondition();
    public static int num = 2;
    public static void main(String []args) {
        Thread a = new Thread(new Runnable(){
            @Override
            public void run() {
                try{
                    for(int i = 2; i <= 10; i=i+2) {
                        lock.lock();
                        while(num == 2) condition.await();
                        System.out.print(i);
                        num = 2; condition.signal();
                        lock.unlock();
                    }
                }catch(Exception e) {
                    e.printStackTrace();
                }
            }
        });

        Thread b = new Thread(new Runnable(){
            @Override
            public void run() {
                try{
                    for(int i = 1; i <= 10; i = i+2){
                        lock.lock();
                        while(num == 1) condition.await();
                        System.out.print(i);
                        num = 1; condition.signal();
                        lock.unlock();
                    }
                }catch(Exception e) {
                    e.printStackTrace();
                }
            }
        });
        a.start(); 
        try{
            Thread.sleep(10);
        }catch(Exception e){
            e.printStackTrace();
        };
        b.start();
    }
}

参考文献

JAVA线程间通信的几种方式

正文到此结束
本文目录