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

或者

在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线程学习之线程创建的更多相关文章

  1. Java多线程学习(二)---线程创建方式

    线程创建方式 摘要: 1. 通过继承Thread类来创建并启动多线程的方式 2. 通过实现Runnable接口来创建并启动线程的方式 3. 通过实现Callable接口来创建并启动线程的方式 4. 总 ...

  2. java基础学习总结——线程(一)

    一.线程的基本概念

  3. Java多线程学习之线程池源码详解

    0.使用线程池的必要性 在生产环境中,如果为每个任务分配一个线程,会造成许多问题: 线程生命周期的开销非常高.线程的创建和销毁都要付出代价.比如,线程的创建需要时间,延迟处理请求.如果请求的到达率非常 ...

  4. Java多线程学习(三)---线程的生命周期

    线程生命周期 摘要: 当线程被创建并启动以后,它既不是一启动就进入了执行状态,也不是一直处于执行状态.在线程的生命周期中,它要经过新建(New).就绪(Runnable).运行(Running).阻塞 ...

  5. Java多线程学习篇——线程的开启

    随着开发项目中业务功能的增加,必然某些功能会涉及到线程以及并发编程的知识点.笔者就在现在的公司接触到了很多软硬件结合和socket通讯的项目了,很多的功能运用到了串口通讯编程,串口通讯编程的安卓端就是 ...

  6. JAVA多线程学习七-线程池

    为什么用线程池 1.创建/销毁线程伴随着系统开销,过于频繁的创建/销毁线程,会很大程度上影响处理效率 例如: 记创建线程消耗时间T1,执行任务消耗时间T2,销毁线程消耗时间T3 如果T1+T3> ...

  7. 【java基础学习】线程

    线程 1. 两种创建方式(继承Thread类和实现Runnable接口) 2. 线程共享资源(建议实现Runnable接口,其好处是:1.多线程之间可以共享资源 2.避免单继承带来的问题 3.数据和代 ...

  8. Java多线程学习总结--线程概述及创建线程的方式(1)

    在Java开发中,多线程是很常用的,用得好的话,可以提高程序的性能. 首先先来看一下线程和进程的区别: 1,一个应用程序就是一个进程,一个进程中有一个或多个线程.一个进程至少要有一个主线程.线程可以看 ...

  9. java SE学习之线程同步(详细介绍)

           java程序中可以允许存在多个线程,但在处理多线程问题时,必须注意这样一个问题:               当两个或多个线程同时访问同一个变量,并且一些线程需要修改这个变量时,那么这个 ...

随机推荐

  1. 公开的免费WebService接口分享

    天气预报Web服务,数据来源于中国气象局 Endpoint  Disco  WSDL IP地址来源搜索 WEB 服务(是目前最完整的IP地址数据) Endpoint  Disco  WSDL 随机英文 ...

  2. 如何建立nfs网络文件系统

    建立网络文件系统的前提:windows与linux虚拟机及开发板三者之间能够互相ping 通.  三者互ping通IP设置举例: 1. 首先,关闭windows的防火墙,然后通过:ufw  disab ...

  3. 【面试题】java面试题整理(有空再贴答案)

    面试题+基础 各家的面试题其实都大同小异, 掌握基础和原理,走到哪都不怕. 基础 leetcode上有一些总结,star数非常高了.贴上url https://github.com/CyC2018/C ...

  4. [Golang] kafka集群搭建和golang版生产者和消费者

    一.kafka集群搭建 至于kafka是什么我都不多做介绍了,网上写的已经非常详尽了. 1. 下载zookeeper  https://zookeeper.apache.org/releases.ht ...

  5. jquery验证规则

    <!DOCTYPE html><html><head><meta charset="utf-8"><title>菜鸟教程 ...

  6. Number()、parseInt()和parseFloat()的区别

    JS中Number().parseInt()和parseFloat()的区别 三者的作用: Number(): 可以用于任何数据类型转换成数值: parseInt(): 函数可解析一个字符串,并返回一 ...

  7. js中的异步与同步,解决由异步引起的问题

    之前在项目中遇到过好多次因为异步引起的变量没有值,所以意识到了认识js中同步与异步机制的重要性 在单线程的js中,异步代码会被放入一个事件队列,等到所有其他代码执行后再执行,而不会阻塞线程. 下面是j ...

  8. 22.2、react生命周期与react脚手架(二)

    一.类:es6 <script type="text/babel"> class Person{ age = 10; constructor(name){ this.n ...

  9. 【node】node连接mongodb操作数据库

    1.下载第三方模块mongodb cnpm install mongodb --save 2.检测是否连接成功 1.引入第三方模块mongodb并创建一个客户端 const MongoClient = ...

  10. Python学习之旅(三十三)

    Python基础知识(32):网络编程(Ⅰ) 网络通信是两台计算机上的两个进程之间的通信,而网络编程就是如何在程序中实现两台计算机的通信 P协议负责把数据从一台计算机通过网络发送到另一台计算机 TCP ...