java thread run and start
在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的更多相关文章
- Java Thread 的 run() 与 start() 的区别
Java Thread 的使用 Java Thread 的 run() 与 start() 的区别 Java Thread 的 sleep() 和 wait() 的区别 1. ...
- 线上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 ...
- java thread start到run:C++源码分析
转:https://hunterzhao.io/post/2018/06/11/hotspot-explore-inside-java-thread-run/ 整体流程 java new Thread ...
- Java Thread之start和run方法的区别
原创转载请注明出处:https://www.cnblogs.com/agilestyle/p/11421515.html start 用start方法来启动线程,真正实现了多线程运行,这时无需等待ru ...
- Java Thread 的 sleep() 和 wait() 的区别
Java Thread 的使用 Java Thread 的 run() 与 start() 的区别 Java Thread 的 sleep() 和 wait() 的区别 1. sleep ...
- JAVA Thread线程异常监控
一.场景描述:单线程程序可以用try...catch捕获程序的异常,而在多线程程序的时候是无法使用try...catch捕获. 示例1:多线程发生异常,无法使用try...catch捕获问题 publ ...
- Java Thread wait, notify and notifyAll Example
Java Thread wait, notify and notifyAll Example Java线程中的使用的wait,notify和nitifyAll方法示例. The Object clas ...
- java: Thread 和 runnable线程类
java: Thread 和 runnable线程类 Java有2种实现线程的方法:Thread类,Runnable接口.(其实Thread本身就是Runnable的子类) Thread类,默认有ru ...
- Java Thread join() 的用法
Java Thread中, join() 方法主要是让调用改方法的thread完成run方法里面的东西后, 在执行join()方法后面的代码.示例: class ThreadTesterA imple ...
随机推荐
- leetcode Add and Search Word - Data structure design
我要在这里装个逼啦 class WordDictionary(object): def __init__(self): """ initialize your data ...
- cpu中断
中断是什么?cpu在中断的时候做了些什么? 答:中断就是中止当前正在执行的工作,而去执行引起中断的事件,当引起中断的事件执行完毕之后,CPU继续执行以前的未执行完的工作. CPU暂时中断当前正在执行的 ...
- java切换VPN让你像幽灵一样出现在全国各地
在很多情况下,有些网络应用的需求会要求模拟人在不同地区访问网站和应用.因而切换IP也就应运而生了,然而IP作为一种稀缺资源不是随便可以获得的.因而会想到应用程序切换VPN来达到全国不同地区访问网络.因 ...
- hdu3709
枚举+数位dp 注意处理数字为0和1的情况. #include <cstdio> #include <cstring> using namespace std; #define ...
- ffmpeg-20160701-git-bin.7z
ESC 退出 0 进度条开关 1 屏幕原始大小 2 屏幕1/2大小 3 屏幕1/3大小 4 屏幕1/4大小 S 下一帧 [ -2秒 ] +2秒 ; -1秒 ' +1秒 下一个帧 -> -5秒 f ...
- Maven 3.3.3 Win10环境下的使用实例(下)
这一篇文章将介绍如何在 Eclipse 中使用 Maven. 我们以 Eclipse Java EE 版本为例,首先要对 IDE 进行一些设置: JDK 环境 Maven 的本地安装路径 Maven ...
- Match:Milking Grid(二维KMP算法)(POJ 2185)
奶牛矩阵 题目大意:给定一个矩阵,要你找到一个最小的矩阵,这个矩阵的无限扩充的矩阵包含着原来的矩阵 思路:乍一看这一题确实很那做,因为我们不知道最小矩阵的位置,但是仔细一想,如果我们能把矩阵都放在左上 ...
- 108. Convert Sorted Array to Binary Search Tree
题目: Given an array where elements are sorted in ascending order, convert it to a height balanced BST ...
- library not found for -lAFNetworking
错误内容如图所示: error:linker command failed with exit code 1(use -v to see invocation) 首先报这个错的情况有很多,所以需要看e ...
- Redis自定义动态字符串(sds)模块(二)
sds模块的具体实现: 1.sdsnewlen 根据参数生成一个sds字符串 sds sdsnewlen(const void *init, size_t initlen) { struct sdsh ...