• Java 线程(1)


    1.创建

    Java种线程的创建方法有三种:

    1)继承Thread对象,实现run()方法

     1 package thread;
     2 
     3 public class MyThread01 extends Thread {
     4 
     5     private static int i = 10;
     6 
     7     @Override
     8     public void run() {
     9         while (i > 0) {
    10             System.out.println(--i);
    11         }
    12     }
    13 
    14     public static void main(String[] args) {
    15         new MyThread01().start();// 注意用start来启动一个线程
    16         new MyThread01().start();
    17         // new MyThread01().run(); 这只是运行了run方法并没有启动线程
    18     }
    19 }

    2)实现Runnable接口,实例化Thread接口

     1 package thread;
     2 
     3 public class MyThread02 implements Runnable {
     4     // private static int i = 10;// 用一个static 变量来实现对资源的共享
     5 
     6     private int i = 10;
     7 
     8     @Override
     9     public void run() {
    10 
    11         while (i > 0) {
    12             System.out.println(Thread.currentThread().getName() + "执行" + (--i));
    13         }
    14     }
    15 
    16     public static void main(String[] args) {
    17 
    18         // Thread thread1 = new Thread(new MyThread02());
    19         // thread1.setName("T1");
    20         // Thread thread2 = new Thread(new MyThread02());
    21         // thread2.setName("T2");
    22         // thread1.start();
    23         // thread2.start();
    24 
    25         // 用同一个MyThread02完成资源共享
    26         MyThread02 thread02 = new MyThread02();
    27         Thread thread11 = new Thread(thread02);
    28         thread11.setName("T1");
    29         Thread thread12 = new Thread(thread02);
    30         thread12.setName("T2");
    31         thread11.start();
    32         thread12.start();
    33 
    34     }
    35 
    36 }

    3)应用程序可以使用Executor框架来创建线程池

    链接:http://www.cnblogs.com/dolphin0520/p/3932921.html

    2.启动

    1)start方法
    用 start方法来启动线程,是真正实现了多线程, 通过调用Thread类的start()方法来启动一个线程,这时此线程处于就绪(可运行)状态,并没有运行,一旦得到cpu时间片,就开始执行run()方法。但要注意的是,此时无需等待run()方法执行完毕,即可继续执行下面的代码。所以run()方法并没有实现多线程。
    2)run方法
    run()方法只是类的一个普通方法而已,如果直接调用Run方法,程序中依然只有主线程这一个线程,其程序执行路径还是只有一条,还是要顺序执行,还是要等待run方法体执行完毕后才可继续执行下面的代码。

    3.线程的生命周期(可用状态)

    1)新建(new)

    2)可运行(runnable):对象线程创建后,其他线程如主线程调用了该兑现的start方法。线程位于可运行线程池中,等待被线程调度,获取CPU。

    3)运行(running):可运行状态的线程获得了cpu时间片,执行代码。

    4.阻塞(block):线程由于某种原因放弃了cpu的还使用权,暂时停止质心,直到线程进入可运行状态,才有机会再次获得cpu timeslice进入运行状态。分三种情况:

    1)等待阻塞:运行的线程执行wait()方法,进入等待队列

    2)同步阻塞:运行的线程在获取对象的同步锁的时候,若该同步锁被其他线程占用,则JVM会把该线程放入锁池。

    3)其他阻塞:执行Thread.sleep()或t.join()时,或者发出了IO请求时,JVM会把线程置为阻塞状态。

    5.死亡(dead):run()或main()方法执行结束或者因为异常退出run(),则结束生命周期。

  • 相关阅读:
    java.lang.NoClassDefFoundError: Lorg/slf4j/Logger;
    jetty9内嵌到应用,并在启动后加载WebApplicationInitializer,可运行jsp
    Gradle Maven 依赖管理
    gradle多模块开发
    Github .gitignore详解
    Could not find or load main class org.gradle.wrapper.GradleWrapperMain解决办法
    SOC 与 ARM
    Codis集群的搭建与使用
    TIDB VS COCKROACHEB
    mysql故障
  • 原文地址:https://www.cnblogs.com/Run-dream/p/5283760.html
Copyright © 2020-2023  润新知