java线程学习之线程创建
线程是程序控制的一个内部数据流。线程的状态转化如下

或者

在java中创建线程有两种方式:
1.实现runnable接口(这个比较好,推荐这个。原因是:用的时候比较灵活,相比较继承Thread类,用接口来实现可以减少资源使用,比较继承也是一种宝贵资源,毕竟Java是单继承多实现)
2.继承Thread类(Thread类实现了Runnable方法)
利用Runnable接口启动线程的方法是:先创建Runnable接口的实现类,然后将实现类的实例作为参数传给Thread的构造方法,最后调用start方法。
利用继承Thread类来启动线程的方法是:先创建Thread类的子类,然后创建子类的实例,最后调用start方法。
两种方式均是利用Thread类的Start方法。
/**
* Causes this thread to begin execution; the Java Virtual Machine
* calls the <code>run</code> method of this thread.
* <p>
* The result is that two threads are running concurrently: the
* current thread (which returns from the call to the
* <code>start</code> method) and the other thread (which executes its
* <code>run</code> method).
* <p>
* It is never legal to start a thread more than once.
* In particular, a thread may not be restarted once it has completed
* execution.
*
* @exception IllegalThreadStateException if the thread was already
* started.
* @see #run()
* @see #stop()
*/
public synchronized void start() {
/**
* This method is not invoked for the main method thread or "system"
* group threads created/set up by the VM. Any new functionality added
* to this method in the future may have to also be added to the VM.
*
* A zero status value corresponds to state "NEW".
*/
if (threadStatus != 0)
throw new IllegalThreadStateException(); /* Notify the group that this thread is about to be started
* so that it can be added to the group's list of threads
* and the group's unstarted count can be decremented. */
group.add(this); boolean started = false;
try {
start0();
started = true;
} finally {
try {
if (!started) {
group.threadStartFailed(this);
}
} catch (Throwable ignore) {
/* do nothing. If start0 threw a Throwable then
it will be passed up the call stack */
}
}
}
jdk中Thread类的start方法
例子一:实现runnable接口
package com.song.test;
public class TestRunnable implements Runnable {
public void run() {
System.out.println("线程启动....");
}
public static void main(String[] args) {
System.out.println("测试线程一....");
TestRunnable test=new TestRunnable(); //创建实现类的实例
Thread t1=new Thread(test); //将实现类的实例作为参数传给Thread的构造方法
t1.start();//调用start方法启动线程
}
}
运行结果:

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

2例子二:继承Thread类
package com.song.test;
public class TestThread01 extends Thread {
public static void main(String[] args) {
System.out.println("开始执行");
TestThread01 test = new TestThread01();
test.start();
}
@Override
public void run() {
System.out.println("用继承Thread的线程已启动");
}
}
结果为:

使用的jdk1.6的解释为:

关于线程终止:是指除守护线程以外的线程全部终止,守护线程是执行后台作业的线程。当进入main方法中执行程序时就已经启动了主线程,在主线程执行过程中创建了另一个线程 A,且利用start()方法启动它,这时是启动两个线程,main线程和A线程。当main方法代码执行完毕,即主线程才为终止。但如果此时线程A仍在执行中,即A线程仍在运行状态不能说此时程序终止,也就是说所以程序均终止后,程序才能终止。
-----------------------------------------------------------------------------------------------------------------------------------------------------------------
能力有限,不喜勿喷,欢迎指错。
java线程学习之线程创建的更多相关文章
- Java多线程学习(二)---线程创建方式
线程创建方式 摘要: 1. 通过继承Thread类来创建并启动多线程的方式 2. 通过实现Runnable接口来创建并启动线程的方式 3. 通过实现Callable接口来创建并启动线程的方式 4. 总 ...
- java基础学习总结——线程(一)
一.线程的基本概念
- Java多线程学习之线程池源码详解
0.使用线程池的必要性 在生产环境中,如果为每个任务分配一个线程,会造成许多问题: 线程生命周期的开销非常高.线程的创建和销毁都要付出代价.比如,线程的创建需要时间,延迟处理请求.如果请求的到达率非常 ...
- Java多线程学习(三)---线程的生命周期
线程生命周期 摘要: 当线程被创建并启动以后,它既不是一启动就进入了执行状态,也不是一直处于执行状态.在线程的生命周期中,它要经过新建(New).就绪(Runnable).运行(Running).阻塞 ...
- Java多线程学习篇——线程的开启
随着开发项目中业务功能的增加,必然某些功能会涉及到线程以及并发编程的知识点.笔者就在现在的公司接触到了很多软硬件结合和socket通讯的项目了,很多的功能运用到了串口通讯编程,串口通讯编程的安卓端就是 ...
- JAVA多线程学习七-线程池
为什么用线程池 1.创建/销毁线程伴随着系统开销,过于频繁的创建/销毁线程,会很大程度上影响处理效率 例如: 记创建线程消耗时间T1,执行任务消耗时间T2,销毁线程消耗时间T3 如果T1+T3> ...
- 【java基础学习】线程
线程 1. 两种创建方式(继承Thread类和实现Runnable接口) 2. 线程共享资源(建议实现Runnable接口,其好处是:1.多线程之间可以共享资源 2.避免单继承带来的问题 3.数据和代 ...
- Java多线程学习总结--线程概述及创建线程的方式(1)
在Java开发中,多线程是很常用的,用得好的话,可以提高程序的性能. 首先先来看一下线程和进程的区别: 1,一个应用程序就是一个进程,一个进程中有一个或多个线程.一个进程至少要有一个主线程.线程可以看 ...
- java SE学习之线程同步(详细介绍)
java程序中可以允许存在多个线程,但在处理多线程问题时,必须注意这样一个问题: 当两个或多个线程同时访问同一个变量,并且一些线程需要修改这个变量时,那么这个 ...
随机推荐
- 深入浅出MySQL++数据库开发、优化与管理维护+第2版+唐汉明 -- 存储引擎 - 数据类型 - 字符集和校验规则 -
create schema deepInMySql;use deepInMySql; -- 查看当前默认存储引擎show variables like '%table_type%'; -- 查看当前数 ...
- python列表的切片操作允许索引超出范围
其余的不说,列表切片操作允许索引超出范围:
- umi中使用scss
在umi中可以直接使用css,但是并不支持scss,我们需要加两个loader, 直接npm安装 node-sass和sass-loader 即可,剩余的事情umi已经帮我们做好了. npm i -- ...
- POI导出复杂的excel;excel公共样式类;excel拼接定制类;数据科学计数法转为普通值
一.excel公共样式类(包含数据科学计数法转为普通值) package com.thinkgem.jeesite.common.utils.excel; import org.apache.poi. ...
- 解决Pycharm更新package出现的问题:AttributeError:module 'pip' has no attribute 'main'
很久一段时间没有更新Pycharm当中的package了,今天打开Pycharm点击package更新,发生了错误,AttributeError:module 'pip' has no attribu ...
- web.py框架之i18n支持
问题: 在web.py的模板文件中, 如何得到i18n的支持? Solution: 项目目录结构: proj/ |- code.py |- i18n/ |- messages.po |- en_US/ ...
- css处理文本溢出
css: .box{ width: 100px; overflow:hidden; white-space:nowrap; text-overflow:ellipsis; } white-space: ...
- laravel之路由
laravel之路由设置 代码如下: 访问就是: 代码附上: <?php /*|--------------------------------------------------------- ...
- 洛谷P3369 【模板】普通平衡树
题目描述 您需要写一种数据结构(可参考题目标题),来维护一些数,其中需要提供以下操作: 插入x数 删除x数(若有多个相同的数,因只删除一个) 查询x数的排名(排名定义为比当前数小的数的个数+1.若有多 ...
- xpinyin模块
import xpinyin s = xpinyin.Pinyin() #一个实例化,以后了解 print(s.get_pinyin('小小军')) #get_pinyin方法,转出来的拼音,每一个汉 ...