zoukankan      html  css  js  c++  java
  • java线程学习之线程创建

    线程是程序控制的一个内部数据流。线程的状态转化如下

    或者

    在java中创建线程有两种方式:

                                  1.实现runnable接口(这个比较好,推荐这个。原因是:用的时候比较灵活,相比较继承Thread类,用接口来实现可以减少资源使用,比较继承也是一种宝贵资源,毕竟Java是单继承多实现)

                                  2.继承Thread类(Thread类实现了Runnable方法)

    利用Runnable接口启动线程的方法是:先创建Runnable接口的实现类,然后将实现类的实例作为参数传给Thread的构造方法,最后调用start方法。

    利用继承Thread类来启动线程的方法是:先创建Thread类的子类,然后创建子类的实例,最后调用start方法。

    两种方式均是利用Thread类的Start方法。

     1 /**
     2      * Causes this thread to begin execution; the Java Virtual Machine
     3      * calls the <code>run</code> method of this thread.
     4      * <p>
     5      * The result is that two threads are running concurrently: the
     6      * current thread (which returns from the call to the
     7      * <code>start</code> method) and the other thread (which executes its
     8      * <code>run</code> method).
     9      * <p>
    10      * It is never legal to start a thread more than once.
    11      * In particular, a thread may not be restarted once it has completed
    12      * execution.
    13      *
    14      * @exception  IllegalThreadStateException  if the thread was already
    15      *               started.
    16      * @see        #run()
    17      * @see        #stop()
    18      */
    19     public synchronized void start() {
    20         /**
    21          * This method is not invoked for the main method thread or "system"
    22          * group threads created/set up by the VM. Any new functionality added
    23          * to this method in the future may have to also be added to the VM.
    24          *
    25          * A zero status value corresponds to state "NEW".
    26          */
    27         if (threadStatus != 0)
    28             throw new IllegalThreadStateException();
    29 
    30         /* Notify the group that this thread is about to be started
    31          * so that it can be added to the group's list of threads
    32          * and the group's unstarted count can be decremented. */
    33         group.add(this);
    34 
    35         boolean started = false;
    36         try {
    37             start0();
    38             started = true;
    39         } finally {
    40             try {
    41                 if (!started) {
    42                     group.threadStartFailed(this);
    43                 }
    44             } catch (Throwable ignore) {
    45                 /* do nothing. If start0 threw a Throwable then
    46                   it will be passed up the call stack */
    47             }
    48         }
    49     }
    jdk中Thread类的start方法

    例子一:实现runnable接口

     1 package com.song.test;
     2 
     3 public class TestRunnable implements Runnable {
     4     public void run() {
     5         System.out.println("线程启动....");
     6     }
     7     public static void main(String[] args) {
     8         System.out.println("测试线程一....");
     9         TestRunnable test=new TestRunnable(); //创建实现类的实例
    10         Thread t1=new Thread(test);   //将实现类的实例作为参数传给Thread的构造方法
    11         t1.start();//调用start方法启动线程
    12     }
    13 }

    运行结果:

    java的jdk1.6对java.lang.Runnable的解释

     2例子二:继承Thread类

     1 package com.song.test;
     2 
     3 public class TestThread01 extends Thread {
     4     public static void main(String[] args) {
     5         System.out.println("开始执行");
     6         TestThread01 test = new TestThread01();
     7         test.start();
     8     }
     9 
    10     @Override
    11     public void run() {
    12         System.out.println("用继承Thread的线程已启动");
    13     }
    14 }

    结果为:

    使用的jdk1.6的解释为:

    关于线程终止:是指除守护线程以外的线程全部终止,守护线程是执行后台作业的线程。当进入main方法中执行程序时就已经启动了主线程,在主线程执行过程中创建了另一个线程 A,且利用start()方法启动它,这时是启动两个线程,main线程和A线程。当main方法代码执行完毕,即主线程才为终止。但如果此时线程A仍在执行中,即A线程仍在运行状态不能说此时程序终止,也就是说所以程序均终止后,程序才能终止。

    -----------------------------------------------------------------------------------------------------------------------------------------------------------------

    能力有限,不喜勿喷,欢迎指错。

    生于忧患,死于安乐
  • 相关阅读:
    现代软件工程 第八章 【需求分析】练习与讨论
    现代软件工程 第七章 【MSF】练习与讨论
    现代软件工程 第六章 【敏捷流程】练习与讨论
    PPT演说技巧
    Mac上最强大的截图软件-xnip
    什么是函数倾轧(name mangling)?
    编程--在线提交系统(Online Judge)
    C++ 的多继承与虚继承
    C++ 中 string和char* 的区别
    编程语言中优先级与结合性
  • 原文地址:https://www.cnblogs.com/songlove/p/10613396.html
Copyright © 2011-2022 走看看