0
点赞
收藏
分享

微信扫一扫

2022年8月17日——Java中线程的创建

START

描述:

Java中线程的创建方法,有两种,一种为继承Thread类,成为Threa的子类;另一种为,实现Runnable类。具体如下所示:

效果展示:

2022年8月17日——Java中线程的创建_优先级

代码演示:

创建线程(两种方法的演示),优先级

创建方法一:

package test.day_08;

/**
* 线程的使用1:
* 继承Thread,为Thread的子类
*/

public class Demo1 extends Thread{

@Override
public void run() {
System.out.println(this.getName());
}

public static void main(String[] args) {
Demo1 demo1 = new Demo1();
demo1.start();
}
}

创建方法二:

package test.day_08;

/**
* 线程的使用2:
* 实现Runnable接口,并重写run方法
*/
public class Demo2 implements Runnable{


@Override
public void run() {
System.out.println("线程");
}

public static void main(String[] args) {
Thread thread = new Thread(new Demo2());
System.out.println(thread.getName());
thread.start();
}
}

优先级:

package test.day_08;

import java.util.Arrays;

/**
* 线程的优先级
* 最高:10
* 最低:1
* 默认:5
*/
public class Demo3 {

public static void main(String[] args) {
// 线程的状态
System.out.println(Arrays.toString(Thread.State.values()));

// 线程的最高优先级(最大)
System.out.println("最高级:"+Thread.MAX_PRIORITY);
// 线程的最低优先级(最小)
System.out.println("最低级:"+Thread.MIN_PRIORITY);
// 线程的默认优先级
System.out.println("默认级:"+Thread.NORM_PRIORITY);
}
}

END
举报

相关推荐

0 条评论