您现在的位置是:首页 > 文章详情

Java入门—多线程

日期:2018-11-11点击:574

线程是比进程还要小的运行单位,一个进程包含多个线程。

什么是多线程

线程的创建

两种方式:

  1. 创建一个Thread类,或者一个Thread子类的对象
  2. 创建一个实现Runnable接口的类的对象
Thread类:
  • Thread是一个线程类,位于java.lang包下


    img_09e5cbe5a84a2e25efbfcb1f06a82aff.png
    image.png
  • Thread类的常用方法


    img_dd260b2f3372291f3eab5e8646835b58.png
    image.png
Runnable接口
  • 只有一个方法run()
  • Runnable是java中用以实现线程的接口
  • 任何实现线程功能的类都必须实现该接口

继承Thread类的方式创建线程类,重写run方法:

package com.imooc.thread; class MyThread extends Thread{ public void run(){ System.out.println(getName()+"该线程正在执行!"); } } public class ThreadTest{ public static void main(String[] args){ System.out.println("主线程1"); MyThread mt = new MyThread(); mt.start(); System.out.println("主线程2"); } } 

Runnable创建线程:
为什么要实现Runnable接口?

因为:

  1. Java不支持多继承
  2. 当你不打算重写Thread类的其他方法
package com.imooc.thread; class PrintRunnable implements Runnable{ @Override public void run() { int i = 1; while (i<=5){ System.out.println(Thread.currentThread().getName()+"正在运行"); i++; } } } public class ThreadTest{ public static void main(String[] args){ PrintRunnable pr = new PrintRunnable(); Thread t1 = new Thread(pr); t1.start(); Thread t2 = new Thread(pr); t2.start(); } } 

线程的状态和生命周期

线程的状态
  • 新建
  • 可运行
  • 正在运行
  • 阻塞
  • 终止
线程的生命周期
img_6387bcbb8cfcabe214c890710d201a6f.png
image.png
sleep方法:在指定的毫秒数内,让线程休眠。(暂停执行)
public static void sleep(long millis) 

参数单位:毫秒
示例:

class MyThread implements Runnable{ @Override public void run() { for(int i = 0; i<10;i++){ System.out.println(Thread.currentThread().getName()+i); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } } } public class Test { public static void main(String[] args){ MyThread m = new MyThread(); Thread t = new Thread(m); t.start(); } } 

调用sleep时需要捕获异常。

join()方法:优先执行线程。(抢占资源)
public final void join() 

join方法的参数为毫秒,代表:超过此时间交出资源使用权。

示例:

class MyThread implements Runnable{ @Override public void run() { for(int i = 0; i<10;i++){ System.out.println(Thread.currentThread().getName()+i); try { Thread.sleep(2000); } catch (InterruptedException e) { e.printStackTrace(); } } } } public class Test { public static void main(String[] args){ MyThread m = new MyThread(); Thread t = new Thread(m); t.start(); try { t.join(1000); } catch (InterruptedException e) { e.printStackTrace(); } for(int j = 0; j<10;j++){ System.out.println("主线程"); } } } 

线程的调度

线程优先级
  • Java为线程类提供了10个优先级
  • 优先级可以用整数1-10表示,超出范围会抛出异常
  • 主线程默认优先级为5
  • 优先级常量
    • MAX_PRIORITY : 最高优先级(10)
    • MIN_PRIORITY : 最低优先级(1)
    • NORM_PRIORITY :默认优先级(5)
img_e36e79b09d6f519564ab414b71461278.png
image.png

测试代码:

class MyThread implements Runnable{ private String name; public MyThread(String name){ this.name = name; } @Override public void run() { for(int i = 0; i<10;i++){ System.out.println(Thread.currentThread().getName()+i+name); } } } public class Test { public static void main(String[] args){ int i = Thread.currentThread().getPriority(); MyThread m = new MyThread("Allen"); Thread t = new Thread(m); MyThread m1 = new MyThread("ming"); Thread t1 = new Thread(m1); t.setPriority(Thread.MAX_PRIORITY); t1.setPriority(Thread.MIN_PRIORITY); int y = t.getPriority(); int y1 = t1.getPriority(); System.out.println("Allen线程优先级"+y); System.out.println("ming线程优先级"+y1); t.start(); t1.start(); } } 

线程同步

多线程运行时的问题:

  1. 各个线程是通过竞争CPU时间而获得运行机会的
  2. 各个线程什么时候得到CPU时间、占用多久,是不可预测的
  3. 一个正在运行着的线程在什么地方被暂停是不确定的

使用synchronized关键字加锁。同一时刻只能单个操作。

synchronized关键字可以用在:成员方法、静态方法、语句块

public synchronized void F(){} public static synchronized void F(){} synchronized (){} 

线程间通信

Java中的线程通信和Python中的差不多,通过中间队列类存储,由消费者和生产者负责放入和取出。


img_c16ba0223efd205a45fa915cc26916cd.png
image.png

wait方法:中断方法的执行,使线程等待
notify方法:唤醒处于等待的某一个线程
notifyAll方法:唤醒处于等待的全部线程

小结

创建线程的两种方式:

  1. 继承Thread类创建线程 new Thread().start()
  2. 实现Runnable接口创建线程 new Thread(new MyRunable()).start()

线程的5种状态:

  1. 新建
  2. 可运行
  3. 运行
  4. 阻塞
  5. 终止

sleep、join、wait、notify、notifyAll方法:

线程优先级:

  • 最高、最低。
  • 获取优先级、设置优先级。
  • 先启动的线程尽管优先级低,也会可能先执行。

线程同步:
synchronized关键字,可以用在:成员方法、静态方法、语句块。

上述文章如有帮助记得点赞呦,关注后第一时间获得最新笔记,持续更新~

原文链接:https://yq.aliyun.com/articles/672361
关注公众号

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。

持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。

转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。

文章评论

共有0条评论来说两句吧...

文章二维码

扫描即可查看该文章

点击排行

推荐阅读

最新文章