1.线程是什么?
线程是操作系统能够进行运算调度的最小单位,被包含在进程之中,是进程中的实际运作单位。一条线程指的是进程中一个单一顺序的控制流,一个进程中可以并发多个线程,每条线程并行执行不同的任务
2.线程的生命周期
java中线程生命周期,直接找到jdk源码中的定义。源码定义的JAVA线程生命周期。
初始化 运行 阻塞 无限时等待 限时等待 销毁 怎么理解呢?或者说什么情况下出现这些生命周期状态? 我觉得还是画张图比较好。
3.怎么new一个线程
public static void main ( String[ ] args) {
Thread thread1 = new Thread ( ) {
@Override
public void run ( ) {
System. out. println ( "这是第一种方式" ) ;
}
} ;
thread1. start ( ) ;
Runnable thread2 = new Runnable ( ) {
@Override
public void run ( ) {
System. out. println ( "这是第二种方式" ) ;
}
} ;
Thread run2 = new Thread ( thread2) ;
run2. start ( ) ;
ExecutorService service = Executors. newFixedThreadPool ( 10 ) ;
service. submit ( new CallTread ( ) ) ;
Runnable thread4 = ( ) - > System. out. println ( "这是第四种方式" ) ;
Thread run4 = new Thread ( thread4) ;
run4. start ( ) ;
}
static class CallTread implements Callable < Integer> {
@Override
public Integer call ( ) throws Exception {
System. out. println ( "第三种" ) ;
return 1 ;
}
} ```
虽然勉强算有4 种,但是大家有没有发现,所有线程运行都得调用start方法。而start方法是哪里的???Thread类里面的啊,是不是我们可以理解成只有一种线程创建的方式,就是Thread类,毕竟殊途同归了啊。