详解Java线程中常用操作(java中实现线程有哪两种基本方法)

网友投稿 235 2022-07-29


目录线程的常用操作守护线程(后台线程)线程串行化线程优先级线程中断

线程的常用操作

设置线程名字:setName()

获取线程名称:getName()

线程唯一Id:getId()

//自定义线程名称

StringthreadName="threadName";

//构造方法方式

Threadthread=newThread(()->{

System.out.println("线程名="+Thread.currentThread().getName());

},threadName);

//set方法方式

//thread.setName(threadName);

System.out.println("线程唯一Id="+thread.getId());

线程启动:start()

判断线程是否存活:isAlive()

//线程启动

thread.start();

System.out.println("是否为存活线程="+thread.isAlive());

线程方法:run() /call()

线程启动后会去调用的方法。线程要做什么就在run/call方法写,不需要直接调用,线程启动后自己会去调用run() /call()。如果程序没有启动线程直接调用run/call,那么就不属于多线程编程,是属于当前线程直接调用普通方法一样。

获取当前线程对象:currentThread()

操作当前线程的非static方法,得先拿到线程对象才可以

//获取当前线程对象

ThreadcurrentThread=Thread.currentThread();

//对当前线程做一些操作

System.out.println(currentThread.getName());

try{

//sleep静态方法则不需要

Thread.sleep(1000);

}catch(InterruptedExceptione){

e.printStackTrace();

}

关于线程的状态控制(生命周期)的操作可以参考上一篇文章。

守护线程(后台线程)

普通线程(用户线程)的守护者,守护线程的任务是为其他的线程提供服务。如果进程中没有了用户线程,那么守护线程也就没有存在的意义,JVM也随之结束。典型的守护线程有JVM的垃圾回收线程,操作系统的启动也会启动各种模块的守护线程。

设置线程为守护线程:setDaeman()

注意:该方法必须在start() 方法之前调用

publicstaticvoidmain(String[]args){

Threadthread=newThread(()->{

System.out.println("线程名="+Thread.currentThread().getName());

try{

Thread.sleep(1000);

}catch(InterruptedExceptione){

e.printStackTrace();

}

//这一句不会打印出来,因为main线程(目前唯一的普通线程)等待1秒后已经结束了

System.out.println("守护线程的状态="+Thread.currentThread().getState());

});

//守护线程

thread.setDaemon(true);

//线程启动

thread.start();

System.out.println("是否为守护线程="+thread.isDaemon());

}

线程串行化

执行join() 方法的线程进入等待唤醒状态(WAITING),直到调用该方法的线程结束后再由等待唤醒状态转为可运行状态(RUNNABLE)。join() 方法是Thread类中的方法,其底层是使用wait() 方法来实现线程等待,待线程isAlive()为false 时才

实现线程的串行化:一个线程调用另一个线程对象的join() 来实现线程串行化执行。

举个例子:一道好菜

publicclassDemoCooking{

publicstaticvoidmain(String[]args){

ThreadmainThread=Thread.currentThread();

//1.买菜

ThreadbuyThread=newThread(newCookingThread(mainThread,"买菜"),"buyThread");

//2.洗菜

ThreadwashThread=newThread(newCookingThread(buyThread,"洗菜"),"washThread");

//3.切菜

ThreadcutThread=newThread(newCookingThread(washThread,"切菜"),"cutThread");

//4.炒菜

ThreadscrambleThread=newThread(newCookingThread(cutThread,"炒菜"),"scrambleThread");

//不受线程启动顺序的影响

scrambleThread.start();

washThread.start();

cutThread.start();

buyThread.start();

// main线程先执行完才可以开始:买菜

System.out.println("开始准备……");

}

publicstaticclassCookingThreadimplementsRunnable{

privatefinalThreadthread;

privatefinalStringjob;

publicCookingThread(Threadthread,Stringjob){

this.thread=thread;

this.job=job;

}

@Override

publicvoidrun(){

Stringname=Thread.currentThread().getName()+":";

try{

thread.join();

System.out.println(name+job+"开始");

Thread.sleep(1000);

System.out.println(GAPhKjMnname+job+"结束");

Thread.sleep(1000);//偷懒下

}catch(InterruptedExceptione){

e.printStackTrace();

}

}

}

}

执行结果:main > buyThread > washThread > cutThread > scrambleThread > 结束

开始准备……buyThread:买菜开始buyThread:买菜结束washThread:洗菜开始washThread:洗菜结束cutThread:切菜开始cutThread:切菜结束scrambleThread:炒菜开始scrambleThread:炒菜结束

线程优先级

设置当前线程的优先级,线程优先级越高,线程可能获得执行的次数越多,java线程的优先级用整数表示,优先级的范围为1-10,默认为5。

setPriority(int)方法:设置线程的优先级。

getPriority方法:获取线程的优先级。

publicstaticvoidmain(String[]args){

Threadthread=newThread(()->{

System.out.println("线程1");

});

thread.setPriority(10);

Threadthread1=newThread(()->{

System.out.println("线程2");

});

thread1.setPriority(1);

thread.start();

thread1.start();

System.out.println("线程默认的优先级为="+Thread.currentThread().getPriority());

}

线程中断

使用interrupt() 方法设置线程中断标志=true,让线程受到“阻塞”时抛出一个中断信号。如果线程处于阻塞、等待唤醒或超时等待状态(Object.wait, Thread.join和Thread.sleep)时,那么它将接收到一个中断异常(InterruptedException),从而提前被结束该状态。反之,如果线程是处于“可运行”(RUNNABLE)状态,那么中断标志将没有作用。

案例一:线程中断有效

publicstaticvoidmain(String[]args){

Threadthread=newThread(()->{

System.out.println("线程1");

try{

//闹钟1分钟后响

Thread.sleep(60000);

System.out.println("闹钟响了");

}catch(InterruptedExceptione){

//提前退出超时等待状态

System.out.println("发生异常,提前醒了,闹钟没响手动关了");

}

System.out.println("继续执行该线程的后续程序……");

});

thread.setPriority(1);

thread.start();

thread.interrupt();

System.out.println("main线程将thread终端状态设置为"+thread.isInterrupted());

}

执行结果:

main线程将thread 终端状态设置为 true线程1发生异常,提前醒了,闹钟没响手动关了继续执行该线程的后续程序……

案例二:线程中断无效

publicstaticvoidmain(String[]args){

Threadthread1=newThread(()->{

System.out.println("线程"+Thread.currentThread().getName());

while(true){

System.out.print(Thread.currentThread().getState()+"\t");

}

});

thread1.start();

thread1.interrupt();

}

执行结果:线程一直打印自己的状态为RUNNABLE。

以上就是详解Java线程中常用操作的详细内容,更多关于Java线程操作的资料请关注我们其它相关文章!


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:java输入空行结束问题(java中输出空格)
下一篇:Java高并发系统限流算法的实现(Java高并发处理)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~