多线程与高并发
- 进程:LOL.exe 点开 登陆进去,这就叫一个进程。他是一个动态的概念。
- 线程:进程中的最小执行单元,就是一个程序中不同的执行路径。
线程的创建方式
public class Thread_001 {
static class MyThread extends Thread{
@Override
public void run() {
System.out.println("Hello Thread");
}
}
static class MyRun implements Runnable{
@Override
public void run() {
System.out.println("Hello Runnable");
}
}
static class MyCall implements Callable{
@Override
public Object call() throws Exception {
System.out.println("Hello Callable");
return "success";
}
}
public static void main(String[] args) {
new MyThread().start();
new Thread(new MyRun()).start();
new Thread(() ->{
System.out.println("Hello Lambda");
}).start();
new Thread(new FutureTask<String>(new MyCall())).start();
ExecutorService service = Executors.newCachedThreadPool();
service.execute(() -> {
System.out.println("Hello ThreadPool");
});
service.shutdown();
}
}
面试题:请说出启动线程的几种种方法?
答:继承Thread;实现Runnable;线程池;FutureTask+实现Callable;Lambda
线程状态
- NEW - 当一个线程刚被创建,还没有调用start()。
- Runnable - 当线程调用start()方法之后。
- Teminated - 当线程顺利执行完成后结束。
- 其中Runnable运行状态中存在状态变迁:
1. TimedWaiting - 按时间等待,时间结束回到Running
2. Waiting - 调用wait()等方法等待,调用notify()等方法回到Running
3. Blocked - 再同步代码块情况下没有获得锁就会阻塞,获得锁就Running
面试题:以上状态哪些是jvm管理的?哪些是操作系统管理的?
答:以上状态全是jvm管理,但是jvm管理也要通过操作系统,因为jvm本身就是跑在操作系统中的一个普通程序
面试题:线程什么状态会被挂起?挂起是否也是一个状态?
答:Running时,cpu上会跑N个线程,cpu隔断时间宠幸一下这个,再隔段时间宠幸一个那个,这是cpu的一个调度,把线程从running扔回去等待就叫线程挂起。所以它不是一个状态。
public class Thread_002 {
public static void main(String[] args) {
Thread t = new MyThread();
System.out.println("MyThread New State:"+t.getState());
t.start();
System.out.println("MyThread Start State:"+t.getState());
try {
t.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("MyThread Join After State:"+t.getState());
}
static class MyThread extends Thread{
@Override
public void run() {
System.out.println("MyThread Run State:"+this.getState());
for (int i = 0; i < 3; i++) {
try {
this.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(i);
}
}
}
}
执行结果:
MyThread New State:NEW
MyThread Start State:RUNNABLE
MyThread Run State:RUNNABLE
0
1
2
MyThread Join After State:TERMINATED
线程方法
- Thread.sleep() - 睡眠,当前线程暂停一段时间让给别的线程去执行,规定时间到了自动复活
public class Thread_003 {
public static void main(String[] args) {
testSleep();
for (int i = 0; i < 6; i++) {
try {
System.out.print("B"+i+" ");
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
static void testSleep(){
new Thread(()->{
for (int i = 0; i < 5; i++) {
System.out.print("A"+i+" ");
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
}
}
执行结果:
B0 A0 B1 B2 B3 A1 B4 B5 A2 A3 A4
- Thread.yield() - 当前线程正在执行的时候停下进入等待队列;有可能你刚刚进入等待队列调度就立刻把你拿出来继续执行;更大的可能还能把那些原来等待的拿出来执行;即yield就是我让出一下cpu你们等待的能不能抢到我不管。
public class Thread_004 {
public static void main(String[] args) {
testYield();
}
static void testYield(){
new Thread(()->{
for (int i = 0; i < 100; i++) {
System.out.print("A"+i+" ");
if(i%10 == 0){
Thread.yield();
}
}
}).start();
new Thread(() ->{
for (int i = 0; i < 100; i++) {
System.out.print("B"+i+" ");
if(i%10 == 0){
Thread.yield();
}
}
}).start();
}
}
执行结果:
...... A0 A1 A2 A3 B0 A4 B1 B2 B3 A5 A6 A7 A8 A9 B4 B5 B6 B7 B8 B9 A10 B10 A11 ......
- Thread.join() - 在自己当前的线程中加入你调用join的线程,当前线程等待;等join的线程执行完之后自己再去执行。(自己join自己没有意义)。
public class Thread_005 {
public static void main(String[] args) {
testJoin();
}
static void testJoin(){
Thread t1 = new Thread(() -> {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
for (int i = 0; i < 3; i++) {
System.out.print("A"+i+" ");
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
Thread t2 = new Thread(() -> {
for (int i = 5; i < 10; i++) {
if(i == 7){
try {
t1.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.print("B"+i+" ");
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
t1.start();
t2.start();
}
}
执行结果:
B5 B6 A0 A1 A2 B7 B8 B9