Java多线程

Thread类本质上是实现了Runnable接口的一个实例,代表一个线程的实例。

启动线程的唯一方法就是通过Thread类的start()实例方法。start()方法是一个native方法,它将启动一个新线程,并执行run()方法。

线程start()方法调用Thread 的native方法 private native void start0();
线程run()调用Thread的run()方法;

如果自己的类已经extends另一个类,就无法直接extends Thread,此时,可以实现一个Runnable接口。

public class MyThread extends OtherClass implements Runnable

{

public void run() {

System.out.println("MyThread.run()");

}

}

//启动MyThread,需要首先实例化一个Thread,并传入自己的MyThread实例:

MyThread myThread = new MyThread();

Thread thread = new Thread(myThread);

thread.start();

//事实上,当传入一个Runnable target参数给Thread后,Thread的run()方法就会调用target.run()

public void run() {

if (target != null) { target.run();

}

}

package com.yuejiesong.javaThreadTest;
//实现Runnable接口,作息线程的实现类
class MyThread implements Runnable{
private String name;
public MyThread(String name){
this.name = name;
}
//复写run方法,作为现场的执行逻辑主题
public void run() {
for (int i = 0; i < 100; i++) {
System.out.println(name + "运行,i = " +i);
}
}
}
public class ThreadRunable {
public static void main(String[] args) {
MyThread mt1 = new MyThread("ThreadA");//实例化对象
MyThread mt2 = new MyThread("ThreadB");//实例化对象
Thread t1 = new Thread(mt1);
Thread t2 = new Thread(mt2);
t1.start();//启动多线程
t2.start();//启动多线程
}
}
--------------------------------------------------------------------
ThreadB运行,i = 0
ThreadA运行,i = 0
ThreadA运行,i = 1
ThreadB运行,i = 1
ThreadA运行,i = 2
ThreadB运行,i = 2
ThreadB运行,i = 3
ThreadA运行,i = 3
ThreadB运行,i = 4
ThreadA运行,i = 4
ThreadB运行,i = 5
ThreadA运行,i = 5
ThreadA运行,i = 6
ThreadB运行,i = 6
ThreadA运行,i = 7
ThreadA运行,i = 8
ThreadA运行,i = 9
----------------------------------------------------------------
package com.yuejiesong.javaThreadTest;
//继承Thread类,作为线程的实现类
class ExThread extends Thread{
private String name;
public ExThread(String name){
this.name = name;
}

@Override
public void run() {
for (int i = 0; i < 100; i++) {
System.out.println(name + "运行,i= "+ i);
}
}
}

public class ThreadExtend {
public static void main(String[] args) {
ExThread mt1 = new ExThread("threadA0");//实例化对象
ExThread mt2 = new ExThread("threadB0");//实例化对象
mt1.start();//调用线程主体
mt2.start();//调用线程主体
}

}
----------------------------------------------------------------
threadA0运行,i= 61
threadA0运行,i= 62
threadA0运行,i= 63
threadB0运行,i= 0
threadA0运行,i= 64
threadB0运行,i= 1
threadA0运行,i= 65
threadB0运行,i= 2
threadA0运行,i= 66
threadB0运行,i= 3
threadA0运行,i= 67
threadB0运行,i= 4
threadA0运行,i= 68
threadB0运行,i= 5
threadA0运行,i= 69
threadA0运行,i= 70
threadA0运行,i= 71
threadA0运行,i= 72
threadB0运行,i= 6
threadA0运行,i= 73
threadB0运行,i= 7
threadA0运行,i= 74
threadB0运行,i= 8
threadA0运行,i= 75
threadB0运行,i= 9
threadA0运行,i= 76
threadB0运行,i= 10
threadA0运行,i= 77
threadB0运行,i= 11
threadA0运行,i= 78
threadB0运行,i= 12
threadA0运行,i= 79
threadB0运行,i= 13
threadA0运行,i= 80
threadB0运行,i= 14
threadA0运行,i= 81
threadB0运行,i= 15
threadB0运行,i= 16
threadB0运行,i= 17
threadA0运行,i= 82
threadB0运行,i= 18
threadB0运行,i= 19
threadA0运行,i= 83
--------------------------------------------------------------------
两个线程实例是交错运行的,哪个线程实例抢到了 CPU 资源,哪个线程就可以运行,所以程序每次的运行结果肯定是不一样的,在线程启动虽然调用的是 start() 方法,但实际上调用的却是 run() 方法定义的主体。

Thread 类和 Runnable 接口

通过 Thread 类和 Runable 接口都可以实现多线程,那么两者有哪些联系和区别呢?下面我们观察 Thread 类的定义。
public class Thread implements Runnable
从 Thread 类的定义可以清楚的发现,Thread 类也是 Runnable 接口的子类,但在Thread类中并没有完全实现 Runnable 接口中的 run() 方法,下面是 Thread 类的部分定义。
Subclasses of <code>Thread</code> should override this method.
@Override
public void run() {
if (target != null) {
target.run();
}
}
在 Thread 类中的 run() 方法调用的是 Runnable 接口中的 run() 方法,也就是说此方法是由 Runnable 子类完成的,所以如果要通过继承 Thread 类实现多线程,则必须覆写 run()。

实际上 Thread 类和 Runnable 接口之间在使用上也是有区别的,如果一个类继承 Thread类,则不适合于多个线程共享资源,而实现了 Runnable 接口,就可以方便的实现资源的共享。

线程的状态变化
要想实现多线程,必须在主线程中创建新的线程对象。任何线程一般具有6种状态,即创建,就绪,运行,等待,阻塞,终止。下面分别介绍一下这几种状态:
创建状态

在程序中用构造方法创建了一个线程对象后,新的线程对象便处于新建状态,此时它已经有了相应的内存空间和其他资源,但还处于不可运行状态。新建一个线程对象可采用Thread 类的构造方法来实现,例如 “Thread thread=new Thread()”。

就绪状态

新建线程对象后,调用该线程的 start() 方法就可以启动线程。当线程启动时,线程进入就绪状态。此时,线程将进入线程队列排队,等待 CPU 服务,这表明它已经具备了运行条件。

运行状态

当就绪状态被调用并获得处理器资源时,线程就进入了运行状态。此时,自动调用该线程对象的 run() 方法。run() 方法定义该线程的操作和功能。

阻塞状态

一个正在执行的线程在某些特殊情况下,如被人为挂起或需要执行耗时的输入/输出操作,会让 CPU 暂时中止自己的执行,进入阻塞状态。在可执行状态下,如果调用sleep(),suspend(),wait() 等方法,线程都将进入阻塞状态,发生阻塞时线程不能进入排队队列,只有当引起阻塞的原因被消除后,线程才可以转入就绪状态。

死亡状态

线程调用 stop() 方法时或 run() 方法执行结束后,即处于死亡状态。处于死亡状态的线程不具有继续运行的能力。

Java 程序每次运行至少启动两个线程,每当使用 Java 命令执行一个类时,实际上都会启动一个 JVM,每一个JVM实际上就是在操作系统中启动一个线程,Java 本身具备了垃圾的收集机制。在Java运行时至少会启动两个线程,一个是 main 线程,另外一个是垃圾收集线程。

-----------------------------------------------------------
获取线程名

package com.yuejiesong.javaThreadTest;
class namedThread implements Runnable{//实现Runnalbe接口
public void run() {
for (int i = 0; i < 3; i++) {
System.out.println(Thread.currentThread().getName()+ "运行,i="+i);//获取当前线程的名称
}
}
}
public class ThreadName {
public static void main(String[] args) {
namedThread nt = new namedThread();//定义Runnable子类对象
new Thread(nt).start();//系统自动设置线程名
new Thread(nt,"线程AA").start();//自己设置线程名
}
}
----------------------------------------------------------
Thread-0运行,i=0
Thread-0运行,i=1
Thread-0运行,i=2
线程AA运行,i=0
线程AA运行,i=1
线程AA运行,i=2
-----------------------------------------------------------
线程的操作方法

线程的强制运行

在线程操作中,可以使用 join() 方法让一个线程强制运行,线程强制运行期间,其他线程无法运行,必须等待此线程完成之后才可以继续执行。

----------------------------------------------------------
package com.yuejiesong.javaThreadTest;
class JoinThread implements Runnable{//实现Runnable接口
public void run() {
for (int i = 0; i < 3; i++) {
System.out.println(Thread.currentThread().getName()+ "运行,i= "+i);//获取当前线程名字
}
}
}
public class ThreadJoin {
public static void main(String[] args) {
JoinThread jt = new JoinThread();//实例化Runnable子类对象
Thread t = new Thread(jt,"线程");//实例化Thread对象
t.start();//启动线程
for (int i = 0; i < 10; i++) {
if(i>5){
try {
t.join();//线程强制运行
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("Main线程运行---- "+i);
}
}

}
----------------------------------
Main线程运行---- 2
Main线程运行---- 3
Main线程运行---- 4
Main线程运行---- 5
线程运行,i= 0
线程运行,i= 1
线程运行,i= 2
Main线程运行---- 6
Main线程运行---- 7
Main线程运行---- 8
Main线程运行---- 9
---------------------------------
线程的休眠
在程序中允许一个线程进行暂时的休眠,直接使用 Thread.sleep() 即可实现休眠。
package com.yuejiesong.javaThreadTest;
class sleep implements Runnable{
public void run() {
for (int i = 0; i < 50; i++) {
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+"运行,i= "+i);//取得当前线程的名字
}
}
}

public class ThreadSleep {
public static void main(String[] args) {
sleep sleep = new sleep();//实现Runnable子类对象
Thread t = new Thread(sleep,"线程");//实例化Thread对象
t.start();//启动线程

}
}
----------------------------------------------------------------------
线程运行,i= 0
线程运行,i= 1
线程运行,i= 2
线程运行,i= 3
线程运行,i= 4
线程运行,i= 5
----------------------------------------------------------------------
package com.yuejiesong.javaThreadTest;
class sleep implements Runnable{
public void run() {
for (int i = 0; i < 5; i++) {
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+"运行,i= "+i);//取得当前线程的名字
}
}
}

public class ThreadSleep {
public static void main(String[] args) {
sleep sleep = new sleep();//实现Runnable子类对象
Thread t = new Thread(sleep,"线程1");//实例化Thread对象
Thread t2 = new Thread(sleep, "线程2");
t.start();//启动线程
t2.start();//启动线程

}
}
-----------------------------------------------------------------------------------
线程2运行,i= 0
线程1运行,i= 0
线程1运行,i= 1
线程2运行,i= 1
线程2运行,i= 2
线程1运行,i= 2
线程1运行,i= 3
线程2运行,i= 3
线程1运行,i= 4
线程2运行,i= 4
---------------------------------------------------------------------------------
中断线程

当一个线程运行时,另外一个线程可以直接通过interrupt()方法中断其运行状态。
package com.yuejiesong.javaThreadTest;

import static java.lang.Thread.*;

class InThread implements Runnable{//实现Runnalbe接口
public void run() {//复写run()方法
System.out.println("1.进入run()方法");
try {
sleep(5000);//线程休眠5秒
System.out.println("2.已经完成了休眠");
} catch (InterruptedException e) {
System.out.println("3. 休眠被终止");
return;//返回调用处
}
System.out.println("4.run()方法正常结束");
}
}

public class ThreadInterrupt {
public static void main(String[] args) {
InThread mt = new InThread();//实例化Runnable子类对象
Thread t = new Thread(mt,"线程");//实例化Thread对象
t.start();//启动线程
try {
sleep(2000);//线程休眠2秒
} catch (InterruptedException e) {
System.out.println("3.休眠被终止...");
}
t.interrupt();//中断线程执行
}
}
---------------------------------------------------------------
1.进入run()方法
3. 休眠被终止
---------------------------------------------------------------
后台线程

在 Java 程序中,只要前台有一个线程在运行,则整个 Java 进程都不会消失,所以此时可以设置一个后台线程,这样即使 Java 线程结束了,此后台线程依然会继续执行,要想实现这样的操作,直接使用 setDaemon() 方法即可。
package com.yuejiesong.javaThreadTest;
class Daemon implements Runnable{//实现Runnable接口
public void run() {//复写run()方法
while (true){
System.out.println(Thread.currentThread().getName()+"在运行。");
}
}
}

public class ThreadDaemon {
public static void main(String[] args) {
Daemon daemon = new Daemon();//实例化Runnable子类对象
Thread t = new Thread(daemon, "线程");//实例化Thread对象
t.setDaemon(true);//此线程在后台运行
t.start();//启动线程
}
}
--------------------------------------------------------------
在线程类 MyThread 中,尽管 run() 方法中是死循环的方式,但是程序依然可以执行完,因为方法中死循环的线程操作已经设置成后台运行。
线程的优先级

在 Java 的线程操作中,所有的线程在运行前都会保持在就绪状态,那么此时,哪个线程的优先级高,哪个线程就有可能会先被执行。
package com.yuejiesong.javaThreadTest;

import static java.lang.Thread.*;

class Priority implements Runnable{//实现Runnable接口
public void run() {//复写run()方法
for (int i = 0; i < 5; i++) {
try {
sleep(500);//线程休眠
} catch (InterruptedException e) {
}
System.out.println(Thread.currentThread().getName()+"运行,i= "+i );//获取当前进程的名字
}
}
}
public class ThreadPriority {
public static void main(String[] args) {
Thread t1 = new Thread(new Priority(), "线程A");//实例化线程对象
Thread t2 = new Thread(new Priority(), "线程B");//实例化线程对象
Thread t3 = new Thread(new Priority(), "线程C");//实例化线程对象
t1.setPriority(Thread.MIN_PRIORITY);//设置最低优先级
t2.setPriority(Thread.MAX_PRIORITY);//设置最高优先级
t3.setPriority(Thread.NORM_PRIORITY);//设置中等优先级
t1.start();//启动线程
t2.start();//启动线程
t3.start();//启动线程
}
}
-------------------------------------------------------------------------
线程B运行,i= 0
线程C运行,i= 0
线程A运行,i= 0
线程B运行,i= 1
线程C运行,i= 1
线程A运行,i= 1
线程B运行,i= 2
线程C运行,i= 2
线程A运行,i= 2
线程B运行,i= 3
线程C运行,i= 3
线程A运行,i= 3
线程B运行,i= 4
线程C运行,i= 4
线程A运行,i= 4
------------------------------------------------------------------------
从程序的运行结果中可以观察到,线程将根据其优先级的大小来决定哪个线程会先运行,但是需要注意并非优先级越高就一定会先执行,哪个线程先执行将由 CPU 的调度决定。

线程的礼让

在线程操作中,也可以使用 yield() 方法将一个线程的操作暂时让给其他线程执行
package com.yuejiesong.javaThreadTest;
class YThread implements Runnable{//实现Runnable接口
public void run() {
for (int i = 0; i < 5; i++) {
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+ "运行i= " +i);
if(i==2){
System.out.println("线程礼让");
Thread.currentThread().yield();//线程礼让
}
}
}
}

public class ThreadYield {
public static void main(String[] args) {
YThread yt = new YThread();
Thread t1 = new Thread(yt,"线程A");
Thread t2 = new Thread(yt,"线程B");
t1.start();
t2.start();
}
}
-------------------------------------------------------------------------
线程A运行i= 0
线程B运行i= 0
线程A运行i= 1
线程B运行i= 1
线程A运行i= 2
线程礼让
线程B运行i= 2
线程礼让
线程A运行i= 3
线程B运行i= 3
线程A运行i= 4
线程B运行i= 4
-------------------------------------------------------------------------
同步以及死锁
一个多线程的程序如果是通过 Runnable 接口实现的,则意味着类中的属性被多个线程共享,那么这样就会造成一种问题,
如果这多个线程要操作同一个资源时就有可能出现资源同步问题。

解决方法:

同步代码块
synchronized(同步对象){
需要同步的代码

----------------------------------------------------------------------------
package com.yuejiesong.javaThreadTest;
class SyncThread implements Runnable{
private int ticket = 5;//假设共有5张票
public void run() {
for (int i = 0; i < 100; i++) {
synchronized (this){//对当前对象进行同步
if(ticket>0){
//加入延时
try {
Thread.sleep(300);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+"卖票:ticket= " + ticket--);
}
}
}
}
}
public class Threadsynchronized {
public static void main(String[] args) {
SyncThread mt = new SyncThread();//定义线程对象
Thread t1 = new Thread(mt);//定义Thread对象
Thread t2 = new Thread(mt);//定义Thread对象
Thread t3 = new Thread(mt);//定义Thread对象
t1.start();//启动线程
t2.start();//启动线程
t3.start();//启动线程
}
}
-----------------------------------------------------------------------------
Thread-0卖票:ticket= 5
Thread-2卖票:ticket= 4
Thread-2卖票:ticket= 3
Thread-2卖票:ticket= 2
Thread-1卖票:ticket= 1
------------------------------------------------------------------------------
同步方法

除了可以将需要的代码设置成同步代码块外,也可以使用 synchronized 关键字将一个方法声明为同步方法。
synchronized 方法返回值 方法名称(参数列表){


package com.yuejiesong.javaThreadTest;
class SyncMethod implements Runnable{
private int ticket = 5;//假设共有5张票
public void run() {
for (int i = 0; i <100; i++) {
this.sale();//调用同步方法
}

}

private synchronized void sale() {//声明同步方法
if(ticket > 0){
try {
Thread.sleep(300);//加入延迟
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+"卖票:ticket "+ticket--);
}
}
}
public class ThreadSyncMethod {
public static void main(String[] args) {
SyncMethod sm = new SyncMethod();//定义线程对象
Thread t1 = new Thread(sm);//定义Thread对象
Thread t2 = new Thread(sm);//定义Thread对象
Thread t3 = new Thread(sm);//定义Thread对象
t1.start();
t2.start();
t3.start();

}
}
--------------------------------------------------------------------------------
Thread-0卖票:ticket 5
Thread-0卖票:ticket 4
Thread-0卖票:ticket 3
Thread-2卖票:ticket 2
Thread-1卖票:ticket 1
-------------------------------------------------------------------------------
从程序运行的结果可以发现,此代码完成了与之前同步代码同样的功能。
死锁

同步可以保证资源共享操作的正确性,但是过多同步也会产生问题。例如,现在张三想要李四的画,
李四想要张三的书,张三对李四说“把你的画给我,我就给你书”,李四也对张三说“把你的书给我,
我就给你画”两个人互相等对方先行动,就这么干等没有结果,这实际上就是死锁的概念。
所谓死锁,就是两个线程都在等待对方先完成,造成程序的停滞,一般程序的死锁都是在程序运行时出现的。

下面以一个简单范例说明这个概念
package com.yuejiesong.javaThreadTest;

import java.util.TreeMap;

class Zhangsan{//定义张三类
public void say(){
System.out.println("张三对李四说:你给我画,我就把书给你。");
}
public void get(){
System.out.println("张三得到画了");
}
}
class Lisi{//定义李四类
public void say(){
System.out.println("李四对张三说:你给过书,我就把画给你。");
}
public void get(){
System.out.println("李四得到书了");
}
}
public class ThreadDeadLock implements Runnable{
private static Zhangsan zs = new Zhangsan();//实例化static类型
private static Lisi ls= new Lisi();//实例化static型对象
private boolean flag = false;//声明标志位,判断哪个先说话

public void run() {//复写run()方法
if(flag){
synchronized (zs){//同步张三
zs.say();
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
synchronized (ls){
zs.get();
}
}
}else {
synchronized (ls){
ls.say();
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
synchronized (zs){
ls.get();
}
}
}

}

public static void main(String[] args) {
ThreadDeadLock t1 = new ThreadDeadLock();
ThreadDeadLock t2 = new ThreadDeadLock();
t1.flag = true;
t2.flag = false;
Thread thA = new Thread(t1);
Thread thB = new Thread(t2);
thA.start();
thB.start();
}
}
----------------------------------------------------------------
张三对李四说:你给我画,我就把书给你。
李四对张三说:你给过书,我就把画给你。
以下代码不再执行,程序进入死锁状态。
---------------------------------------------------------------

原文地址:https://www.cnblogs.com/songyuejie/p/12535806.html