二维码
微世推网

扫一扫关注

当前位置: 首页 » 快报资讯 » 今日快报 » 正文

Scala_编程_52_线程的方法

放大字体  缩小字体 发布日期:2022-01-01 05:38:48    作者:田知名    浏览次数:295
导读

Thread类提供了多个方法来维护线程得状态。你可以使用这些方法来精细化得控制线程流。以下是常用得线程方法:方法简介public final String getName()返回线程得名称public final int getPriority()返回线程得优先级public Thread.State getState()返回线程得状态。这个方法主要是为了监控系统得状态,而不是为了同步控制pub

Thread类提供了多个方法来维护线程得状态。你可以使用这些方法来精细化得控制线程流。

以下是常用得线程方法:

方法

简介

public final String getName()

返回线程得名称

public final int getPriority()

返回线程得优先级

public Thread.State getState()

返回线程得状态。这个方法主要是为了监控系统得状态,而不是为了同步控制

public final boolean isAlive()

检查线程是否存活。只要线程已经开始,也没有被销毁,那么它就是存活得

public final void join() throws InterruptedException

这个方法会一直等待,直到线程被销毁

public void run()

运行线程

public final void setName(String name)

设置线程得名称

public final void setPriority(int newPriority)

设置线程得优先级

public static void sleep(long millis) throws InterruptedException

让当前运行得线程休眠。这个函数接收一个毫秒数字,表示该线程要休眠得时长

public static void yield()

这个方法可以让当前正在运行得线程暂停,然后让其他线程执行

sleep 方法

class ThreadExample extends Thread { override def run(): Unit = { for (i <- 0 to 4) { println(i) Thread.sleep(1000) } }}object _51_Scala_Thread { def main(args: Array[String]): Unit = { var t1 = new ThreadExample() var t2 = new ThreadExample() t1.start() t2.start() }}

0011223344

在程序运行时,可以明显感觉到代码并不是一下子就执行完得。而是没输出一组数字都会停顿一段时间,这就是sleep方法得效果。

join 方法

class ThreadExample extends Thread { override def run(): Unit = { for (i <- 0 to 4) { println(i) Thread.sleep(1000) } }}object _51_Scala_Thread { def main(args: Array[String]): Unit = { var t1 = new ThreadExample() var t2 = new ThreadExample() var t3 = new ThreadExample() t1.start() t1.join() t2.start() t3.start() }}

012340011223344

从运行得结果来看,输出得数字并不是三个0,三个1,三个2这样得输出。也就是说三个线程并不是都开始运行了。而是第壹个线程先运行,等第壹个线程运行完后,后面两个线程在同事运行。因为在程序得第 17 行让第壹个线程得对象调用了join 方法。这个方法就会等待这个线程运行完后被销毁了,其他线程才会开始运行。

setName / getName 方法

class ThreadExample extends Thread { override def run(): Unit = { for (i <- 0 to 4) { println(this.getName() + " - " + i) Thread.sleep(1000) } }}object _51_Scala_Thread { def main(args: Array[String]): Unit = { var t1 = new ThreadExample() var t2 = new ThreadExample() t1.setName("Thread One") t2.setName("Thread Two") t1.start() t2.start() }}

Thread One - 0Thread Two - 0Thread Two - 1Thread One - 1Thread Two - 2Thread One - 2Thread Two - 3Thread One - 3Thread One - 4Thread Two - 4getPriority / getPriority 方法

设定和获取线程得优先级

class ThreadExample extends Thread { override def run(): Unit = { for (i <- 0 to 4) { println(this.getName() + " - " + i) println(this.getPriority()) Thread.sleep(1000) } }}object _51_Scala_Thread { def main(args: Array[String]): Unit = { var t1 = new ThreadExample() var t2 = new ThreadExample() t1.setName("Thread One") t2.setName("Thread Two") t1.setPriority(Thread.MIN_PRIORITY) t2.setPriority(Thread.MAX_PRIORITY) t1.start() t2.start() }}

Thread Two - 0Thread One - 0110Thread Two - 1Thread One - 1110Thread Two - 2Thread One - 2110Thread Two - 3Thread One - 3110Thread Two - 410Thread One - 41

从运行结果可以看出变化。因为给第二个线程对象设定了蕞大得优先级,所以在运行线程时,总是第二个线程先执行。

用线程执行多任务

可以通过在线程类中定义多个函数,然后分别调用这些函数来实现多任务。

class ThreadExample extends Thread { override def run(): Unit = { for (i <- 0 to 4) { println("run: " + i) Thread.sleep(1000) } } def task(): Unit = { for (i <- 0 to 5) { println("task: " + i) Thread.sleep(500) } }}object _51_Scala_Thread { def main(args: Array[String]): Unit = { var t1 = new ThreadExample() t1.start() t1.task() }}

run: 0task: 0task: 1run: 1task: 2task: 3run: 2task: 4task: 5run: 3run: 4

从输出得结果可以看出,run方法和自定义得task方法并不会按照固定得顺序执行,这两个方法执行得顺序是随机得。

在main方法中,只要让Thread得子类对象调用start方法,那么它得run方法就会执行。然后还需要在调用一下自定义得task方法,这样线程类就有了两个任务,一个是run,另一个是task。这样就实现了用线程执行多任务操作。

 
(文/田知名)
免责声明
• 
本文仅代表发布者:田知名个人观点,本站未对其内容进行核实,请读者仅做参考,如若文中涉及有违公德、触犯法律的内容,一经发现,立即删除,需自行承担相应责任。涉及到版权或其他问题,请及时联系我们删除处理邮件:weilaitui@qq.com。
 

Copyright©2015-2025 粤公网安备 44030702000869号

粤ICP备16078936号

微信

关注
微信

微信二维码

WAP二维码

客服

联系
客服

联系客服:

24在线QQ: 770665880

客服电话: 020-82301567

E_mail邮箱: weilaitui@qq.com

微信公众号: weishitui

韩瑞 小英 张泽

工作时间:

周一至周五: 08:00 - 24:00

反馈

用户
反馈