Java多线程面试题

Java多线程面试题

1.多线程顺序执行 使用join协调线程执行顺序

/**
 * 顺序打印 A B C 使用 join 来协调执行顺序
 */
public class ThreadTest {

    public static void main(String[] args) throws InterruptedException {
        Thread A = new Thread(new MyThread("A"));
        Thread B = new Thread(new MyThread("B"));
        Thread C = new Thread(new MyThread("C"));

        A.start();
        A.join();
        B.start();
        B.join();
        C.start();
        C.join();
    }
}

class MyThread implements Runnable {

    private String name;

    public MyThread(String name) {
        this.name = name;
    }

    @Override
    public void run() {
        System.out.println(name);
    }
}

2.多线程顺序执行 使用 wait & notifyAll

public class ThreadTest2 {

    static volatile int state = 0;

    static final Object lock = new Object();

    public static void main(String[] args) {

        new Thread(new Runnable() {
            @Override
            public void run() {
                synchronized (lock) {
                    while (true) {
                        if (state != 2) {
                            try {
                                lock.wait();
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        }
                        if (state == 2) {
                            System.out.println("C");
                            state++;
                            lock.notifyAll();
                            break;
                        }
                    }

                }
            }
        }).start();

        new Thread(new Runnable() {
            @Override
            public void run() {
                synchronized (lock) {
                    while (true) {
                        if (state != 1) {
                            try {
                                lock.wait();
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        }
                        if (state == 1) {
                            System.out.println("B");
                            state++;
                            lock.notifyAll();
                            break;
                        }
                    }

                }
            }
        }).start();

        new Thread(new Runnable() {
            @Override
            public void run() {
                if (state == 0) {
                    synchronized (lock) {
                        System.out.println("A");
                        state++;
                        lock.notifyAll();
                    }
                }
            }
        }).start();

    }
}

3.两个线程交替打印 1-99

/**
 * 两个线程 交替打印字符串
 */
public class ThreadTest4 {

    static final Object lock = new Object();
    static int num1 = 0;
    static int num2 = 1;
    static int end = 100;
    volatile static int state = 0;

    public static void main(String[] args) {

        Thread a = new Thread() {
            public void run() {
                synchronized (lock) {
                    while (true) {
                        if (state == 1) {
                            try {
                                lock.wait();
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        }
                        state = 1;
                        if (num1 >= end) {
                            break;
                        }
                        System.out.println(num1);
                        num1 += 2;
                        lock.notify();
                    }
                }

            }
        };


        Thread b = new Thread() {
            public void run() {
                synchronized (lock) {
                    while (true) {
                        if (state == 0) {
                            try {
                                lock.wait();
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        }
                        state = 0;
                        if (num2 >= end) {
                            break;
                        }
                        System.out.println(num2);
                        num2 += 2;
                        lock.notify();
                    }
                }

            }
        };
        b.start();
        a.start();
    }
}

4.多线程求和 1+2+3+...+99

package com.rocketmq.thread;

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;

public class ThreadSum extends Thread {

    static int totalCount;

    private int start;
    private int end;
    private CyclicBarrier barrier;

    public ThreadSum(int start, int end, CyclicBarrier barrier) {
        this.start = start;
        this.end = end;
        this.barrier = barrier;
    }

    @Override
    public void run() {
        int count = 0;
        for (int i = start; i < end + 1; i++) {
            count += i;
        }

        //需要获取锁对 totalCount 累加
        synchronized (ThreadSum.class) {
            totalCount += count;
        }

        try {
            // 进入到同步屏障阻塞等待所有线程进入同步屏障然后返回
            barrier.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (BrokenBarrierException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        int max = 20000; //对从1到20,000求和
        int nThreads = 10;
        CyclicBarrier barrier = new CyclicBarrier(nThreads, new Runnable() {
            @Override
            public void run() {
                System.out.println(ThreadSum.totalCount);
            }
        });

        for (int i = 0; i < nThreads; i++) {
            int fromInt = max * i / nThreads + 1; //边界条件
            int toInt = max * (i + 1) / nThreads; //边界条件
            new ThreadSum(fromInt, toInt, barrier).start();
        }

    }
}

=========END=========

转载于:https://my.oschina.net/xinxingegeya/blog/1797425

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值