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=========