Java14 多线程编程

线程vs进程

线程

• thread().start();

• thread(Runnable a).start();

Thread.currentThread.getName();

进程

Runtime  runtime  = Runtime.getRuntime();

runtime.exec("notepad");

进程
public class ProcessModel {
    public static void main(String[] args) throws Exception{
        Runtime runtime = Runtime.getRuntime();
        runtime.exec("notepad");
    }
}

运行结果:

打开记事本

 

并行vs并发

 

 

synchronized

同步代码块vs同步方法

  •  /'sɪŋkrənaɪzd/  同步的;同步化的

synchronized(this){

}

 

public synchronized void fun(){

}

 

Lock

lk.lock();

lk.unlock();

 

synchronized    wait(); notify();

lock Condition     await(); signal();

 

 

Thread.sleep(5);                                  线程休眠

Thread.yield();                                     线程礼让

Thread.currentThread().getName();    当前线程名字

thread.join();                                         强制加入线程;

thread.setDaemon();                            设置后台线程;

thread.setPriority(10);                           设置线程优先级;

 

posted @ 2021-06-01 17:04  legendary_tm  阅读(40)  评论(0编辑  收藏  举报