在java中继承Thread,线程启动有两中方法:start()和run()。下面简单介绍一下两者的区别。

start():启动一个线程,此时线程处于就绪状态,然后调用Thread对象的run()方法;不能多次启动一个线程。在main方法执行结束后,由于start()方法创建的线程没有运行结束,因此主线程未能退出,直到线程thread也执行完毕.这里要注意,默认创建的线程是用户线程(非守护线程)。多次调用start()方法会出现java.lang.IllegalThreadStateException异常。

run():在本线程内调用Thread对象的run()方法;可以重复多次调用(因为只是调用一个方法而已)。程序依旧只有主线程这一条线路。

下面是测试代码:

package mutiThread;

public class ExtendThread extends Thread {
private String name; public ExtendThread(){
} public ExtendThread(String name){
this.name = name;
} public void run(){
int i = 0;
while(i < 2){
System.out.println("Thread " + this.name + " is running " + i);
i ++;
} } public static void main(String args[]){
ExtendThread et1 = new ExtendThread("A");
//第一种
//说明: run()和其他方法的调用没任何不同,main方法按顺序执行了它,并打印出最后一句。
//可能结果:Thread A is running 0
//Thread A is running 1
//main thread is over
//et1.run();
//et1.run(); //无异常,相当于调用了两次run()方法 //第二种
//说明: start()方法重新创建了一个线程,在main方法执行结束后,由于start()方法创建的线程没有运行结束,
//因此主线程未能退出,直到线程thread也执行完毕.这里要注意,默认创建的线程是用户线程(非守护线程)
//可能结果:Thread A is running 0
//Thread A is running 1
//main thread is over
//或者:main thread is over
//Thread A is running 0
//Thread A is running 1
//et1.start();
//et1.start();//第二次启动时java.lang.IllegalThreadStateException异常。 //第三种
//1、为什么没有打印出4句呢?因为我们将thread线程设置为了daemon(守护)线程,程序中只有守护线程存在的时候,是可以退出的,所以只打印了七句便退出了
//2、当java虚拟机中有守护线程在运行的时候,java虚拟机会关闭。当所有常规线程运行完毕以后,
//守护线程不管运行到哪里,虚拟机都会退出运行。所以你的守护线程最好不要写一些会影响程序的业务逻辑。否则无法预料程序到底会出现什么问题
//可能结果:main thread is over
//Thread A is running 0
//或者:main thread is over
//et1.setDaemon(true);
//et1.start(); //第四种
//用户线程可以被System.exit(1)强制kill掉
//可能结果:main thread is over
//Thread A is running 0
//Thread A is running 1
//或者main thread is over
et1.start();
System.out.println("main thread is over");
System.exit(1);
}
}

参考:http://www.cnblogs.com/linjiqin/archive/2011/04/10/2011272.html

java thread run and start的更多相关文章

  1. Java Thread 的 run() 与 start() 的区别

    Java Thread 的使用 Java Thread 的 run() 与 start() 的区别 Java Thread 的 sleep() 和 wait() 的区别             1. ...

  2. 线上zk节点报org.apache.zookeeper.server.NIOServerCnxnFactory.run(NIOServerCnxnFactory.java:187) at java.lang.Thread.run(libgcj.so.10)

    线上zk做配置管理,最近突然发现两个节点一直在刷下边 java.nio.channels.CancelledKeyException    at gnu.java.nio.SelectionKeyIm ...

  3. java thread start到run:C++源码分析

    转:https://hunterzhao.io/post/2018/06/11/hotspot-explore-inside-java-thread-run/ 整体流程 java new Thread ...

  4. Java Thread之start和run方法的区别

    原创转载请注明出处:https://www.cnblogs.com/agilestyle/p/11421515.html start 用start方法来启动线程,真正实现了多线程运行,这时无需等待ru ...

  5. Java Thread 的 sleep() 和 wait() 的区别

    Java Thread 的使用 Java Thread 的 run() 与 start() 的区别 Java Thread 的 sleep() 和 wait() 的区别       1. sleep ...

  6. JAVA Thread线程异常监控

    一.场景描述:单线程程序可以用try...catch捕获程序的异常,而在多线程程序的时候是无法使用try...catch捕获. 示例1:多线程发生异常,无法使用try...catch捕获问题 publ ...

  7. Java Thread wait, notify and notifyAll Example

    Java Thread wait, notify and notifyAll Example Java线程中的使用的wait,notify和nitifyAll方法示例. The Object clas ...

  8. java: Thread 和 runnable线程类

    java: Thread 和 runnable线程类 Java有2种实现线程的方法:Thread类,Runnable接口.(其实Thread本身就是Runnable的子类) Thread类,默认有ru ...

  9. Java Thread join() 的用法

    Java Thread中, join() 方法主要是让调用改方法的thread完成run方法里面的东西后, 在执行join()方法后面的代码.示例: class ThreadTesterA imple ...

随机推荐

  1. [转载]C++虚函数浅析

    原文:http://glgjing.github.io/blog/2015/01/03/c-plus-plus-xu-han-shu-qian-xi/ 感谢:单刀土豆 C++虚函数浅析 JAN 3RD ...

  2. ls常用选项总结

    参考: http://billie66.github.io/TLCL/book/index.html ls - List directory contents 选项 长选项 描述 -a --all 列 ...

  3. cpu和io进程调度时间

    [题目] 在一个单CPU的计算机系统中,有两台外部设备R1.R2和三个进程P1.P2.P3.系统采用可剥夺式优先级的进程调度方案,且所有进程可以并行使用I/O设备,三个进程的优先级.使用设备的先后顺序 ...

  4. SQL Server 2005中的分区表

    记录笔记: 转自 猪八戒学做网站 SQL Server 2005中的分区表(一):什么是分区表?为什么要用分区表?如何创建分区表? SQL Server 2005中的分区表(二):如何添加.查询.修改 ...

  5. Java for LeetCode 211 Add and Search Word - Data structure design

    Design a data structure that supports the following two operations: void addWord(word)bool search(wo ...

  6. SQL Server 2008登录错误:无法连接到(local)的解决方法

    1.服务器类型我们选择了“数据库引擎”时,查找里面的可登录用户名是没有的,下边的服务器名称只显示为“(local)”,连“Windows 身份验证”都无法登录. 如果朋友们和我出错的问题是一样请看下面 ...

  7. js闭包问题

    function picLinkInit(parentClassName, imgW, imgH, childClassObjs) { var $match = $(parentClassName); ...

  8. 【leetcode】Substring with Concatenation of All Words (hard) ★

    You are given a string, S, and a list of words, L, that are all of the same length. Find all startin ...

  9. 【linux】ps

    来源:http://blog.chinaunix.net/uid-25681671-id-3201927.html Linux下PS命令详解 要对系统中进程进行监测控制,查看状态,内存,CPU的使用情 ...

  10. 史上最全web.xml配置文件元素详解

    一.web.xml配置文件常用元素及其意义预览 <web-app> <!--定义了WEB应用的名字--> <display-name></display-na ...