多线程语言的好处是,在同一时间让 cpu 处理多个事情。
充分的利用cpu多核多线程的资源优势。
程序也会执行的更快!
支持多线程的语言有特别多,比如java、python 等等,但是也有单线程语言 如js
class RunnableDemo implements Runnable {
private Thread t;
private final String threadName;
RunnableDemo( String name) {
threadName = name;
System.out.println("创建 " + threadName );
}
public void run() {
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
System.out.println("运行 " + threadName );
}
public void start () {
System.out.println("启动 " + threadName );
if (t == null) {
t = new Thread (this, threadName);
t.start ();
}
}
}
public class Main {
// 开启一个主线程
public static void main(String[] args) {
new RunnableDemo( "线程1").start(); // 开启第2个线程
new RunnableDemo( "线程2").start(); // 开启第3个线程
}
}
标签:语言,单线程,threadName,JS,start,线程,new,RunnableDemo,public
From: https://www.cnblogs.com/dingshaohua/p/17600147.html