版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/yerenyuan_pku/article/details/82833209
多线程的学习真的接近尾声了,真幸运,走到这步蛮不容易的,总觉得应该多写点什么,算了吧!还是记录下有关多线程方面的知识点吧!下面我一一讲解下线程类的其他方法。
setDaemon(boolean)
setDaemon(boolean):将该线程标记为守护线程或用户线程。当正在运行的线程都是守护线程时,Java虚拟机退出。注意:该方法必须在启动线程前调用。
我们看到的都是前台线程,主线程就是前台线程。后台线程,应该也是守护线程吧!后台线程和前台线程开启、运行都没有什么区别,但是在结束时是有区别的,前台线程一经消亡,后台线程(不管什么状态)也会跟着消亡,即后台依赖前台。现给出一个示例程序,大家可以参考一下。
class StopThread implements Runnable
{
private boolean flag = true;
public synchronized void run()
{
while (flag)
{
try
{
wait(); // t1、t2
}
catch (InterruptedException e) // 凡是线程处于冻结状态,
{
System.out.println(Thread.currentThread().getName() + "..................." + e.toString());
flag = false;
}
System.out.println(Thread.currentThread().getName() + "......hello");
}
}
public void changeFlag()
{
flag = false;
}
}
class StopThreadDemo
{
public static void main(String[] args)
{
StopThread st = new StopThread();
Thread t1 = new Thread(st);
Thread t2 = new Thread(st);
t1.start();
t2.setDaemon(true); // 将t2线程标记为后台线程。垃圾回收线程就是后台线程。
t2.start();
for (int x = 1; x <= 50; x++)
{
if (x == 40)
{
t1.interrupt(); // 将t1中断。
}
System.out.println("main....." + x);
}
System.out.println("over");
}
}
运行以上程序,程序依然会停止。
join()
join():当A线程执行到了B线程的join()时,A就会等待,等B线程都执行完,A才会执行。join()可以用来临时加入线程执行。
现给出一个示例程序,大家可以参考一下。
class Demo implements Runnable
{
public void run()
{
for (int x = 1; x <= 40; x++)
{
System.out.println(Thread.currentThread().getName() + "......" + x);
}
}
}
class JoinDemo
{
public static void main(String[] args) throws InterruptedException
{
Demo d = new Demo();
Thread t1 = new Thread(d);
Thread t2 = new Thread(d);
t1.start();
t2.start();
t1.join(); // main线程等待t1线程终止。即主线程冻结。
for (int x = 1; x <= 40; x++)
{
System.out.println("main......" + x);
}
}
}
setPriority()
setPriority():更改线程的优先级。这里给出一个示例程序,大家可以参考一下。
class Demo implements Runnable
{
public void run()
{
for (int x = 1; x <= 40; x++)
{
System.out.println(Thread.currentThread().toString() + "......" + x);
}
}
}
class JoinDemo
{
public static void main(String[] args) throws InterruptedException
{
Demo d = new Demo();
Thread t1 = new Thread(d);
Thread t2 = new Thread(d);
t1.start();
t2.start();
// t1.join(); // main线程等待t1线程终止。即主线程冻结。
t1.setPriority(Thread.MAX_PRIORITY); //设置进程优先级,默认为5
for (int x = 1; x <= 40; x++)
{
System.out.println(Thread.currentThread().toString() + "......" + x);
}
}
}
yield():
yield():将CPU的执行权释放出去,作用:让线程都有平均运行的机会。这里同样给出一个示例程序,大家可以参考一下。
class Demo implements Runnable
{
public void run()
{
for (int x = 1; x <= 40; x++)
{
System.out.println(Thread.currentThread().toString() + "......" + x);
Thread.yield(); // 将CPU的执行权释放出去,作用:让线程都有平均运行的机会
}
}
}
class JoinDemo
{
public static void main(String[] args) throws InterruptedException
{
Demo d = new Demo();
Thread t1 = new Thread(d);
Thread t2 = new Thread(d);
t1.start();
t2.start();
// t1.join(); // main线程等待t1线程终止。即主线程冻结。
t1.setPriority(Thread.MAX_PRIORITY); //设置进程优先级,默认为5
for (int x = 1; x <= 40; x++)
{
System.out.println(Thread.currentThread().toString() + "......" + x);
}
}
}
多线程常见写法
实际开发时,怎么使用多线程呢?以下例进行讲解,假设main()里有3段循环代码运行。可用多线程来实现(代码如下):
class ThreadTest {
public static void main(String[] args) {
new Thread() {
public void run() {
for(int x = 0; x < 100; x++) {
System.out.println(Thread.currentThread().getName()+"....."+x);
}
}
}.start();
for(int x = 0; x < 100; x++) {
System.out.println(Thread.currentThread().getName()+"....."+x);
}
Runnable r = new Runnable() {
public void run() {
for(int x = 0; x < 100; x++) {
System.out.println(Thread.currentThread().getName()+"....."+x);
}
}
};
new Thread(r).start();
}
}
不知道你有没有想过,如果程序写成这样,运行后的结果是什么?
class ThreadTest
{
public static void main(String[] args)
{
//------------------问结果是什么?------------------
new Thread(new Runnable() {
public void run()
{
System.out.println("1Runnable...run...");
}
}) {
public void run()
{
System.out.println("2subThread...run...");
}
}.start();
}
}
答案很明显,结果为:
2subThread…run…