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程序中可以允许存在多个线程,但在处理多线程问题时,必须注意这样一个问题: 当两个或多个线程同时访问同一个变量,并且一些线程需要修改这个变量时,那么这个 ...
随机推荐
- 应用程序连接hbase报错:java.net.SocketTimeoutException: callTimeout=60000
背景说明: 今天对生产环境hbase增加了节点,下午的时候一个同事反馈,应用程序后台报错,如下: Tue Feb 26 17:35:35 CST 2019, null, java.net.Socket ...
- unity3d IL2CPP for android
unity3d 使用IL2Cpp导出android工程报以下错误解决方法 重点是这块 “”System.Reflection.TargetInvocationException: ���õ�Ŀ�귢�� ...
- 关于Kafka broker IO的讨论
Apache Kafka是大量使用磁盘和页缓存(page cache)的,特别是对page cache的应用被视为是Kafka实现高吞吐量的重要因素之一.实际场景中用户调整page cache的手段并 ...
- 手写LRU算法
import java.util.LinkedHashMap; import java.util.Map; public class LRUCache<K, V> extends Link ...
- 《转载》强大全面的C++框架和库推荐!
C++ 资源大全 关于 C++ 框架.库和资源的一些汇总列表,内容包括:标准库.Web应用框架.人工智能.数据库.图片处理.机器学习.日志.代码分析等. 标准库 C++标准库,包括了STL容器,算法和 ...
- Spring框架介绍及使用
Spring框架—控制反转(IOC)1 Spring框架概述1.1 什么是Spring1.2 Spring的优点1.3 Spring的体系结构2 入门案例:(IoC)2.1导入jar包2.2目标类2. ...
- PHP连接MySQL数据库的三种方式(mysql、mysqli、pdo)
PHP与MySQL的连接有三种API接口,分别是:PHP的MySQL扩展 .PHP的mysqli扩展 .PHP数据对象(PDO) ,下面针对以上三种连接方式做下总结,以备在不同场景下选出最优方案. P ...
- CF 634A Island Puzzle
A. Island Puzzle time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...
- shell脚本监控网站状态
shell脚本监控网站状态 #!/bin/sh date=`date +"%Y%m%d-%H%M"` title="status" contentFail=&q ...
- 在php cli下可以使用 STDIN 来实现标准输入
简单的例子: echo "请输入一个数字:"; $num = trim(fgets(STDIN)); echo "请再输入一个数字:"; $num1 = ...