max.setPriority(Thread.MAX_PRIORITY);//将线程max设置为最大值10
min.setPriority(Thread.MIN_PRIORITY);//将线程min设置为最小大值1
min.start();
norm.start();
max.start();
}
}
1.通过实现 Runnable 接口来创建线程
创建一个线程,最简单的方法是创建一个实现 Runnable 接口的类。为了实现 Runnable,一个类只需要执行一个方法调用 run(),线程创建之后调用它的 start() 方法它才会执行。
Thread 定义了几个构造方法,下面的这个是我们经常使用的:
//threadOb 是一个实现 Runnable 接口的类的实例
//threadName是线程的名字
Thread(Runnable threadOb,String threadName);
代码示例:
public class RunnableDemo implements Runnable{
private Thread thread;
private String threadName;
RunnableDemo( String name) {
threadName = name;
System.out.println("Creating " + threadName );
}
public void run() {
System.out.println("Running " + threadName );
try {
for(int i = 1; i <= 2; i++) {
System.out.println("Thread: " + threadName + “,第” + i + “次执行”);
// 让线程睡眠一会
Thread.sleep(50);
}
}catch (InterruptedException e) {
System.out.println(“Thread " + threadName + " interrupted.”);
}
System.out.println(“Thread " + threadName + " exiting.”);
}
public void start () {
System.out.println("Starting " + threadName );
if (thread == null) {
thread = new Thread (this, threadName);
thread.start ();
}
}
public static void main(String[] args) {
RunnableDemo R1 = new RunnableDemo( “Thread-1”);
R1.start();
RunnableDemo R2 = new RunnableDemo( “Thread-2”);
R2.start();
}
}
执行结果:
2.通过继承Thread来创建线程
创建一个类,该类继承 Thread 类,然后创建一个该类的实例。继承类必须重写 run() 方法,该方法是新线程的入口点。它也必须调用 start() 方法才能执行。该方法尽管被列为一种多线程实现方式,但是本质上也是实现了 Runnable 接口的一个实例。
代码示例:
public class ThreadDemo extends Thread {
private Thread thread;
private String threadName;
ThreadDemo(String name) {
this.threadName = name;
System.out.println("Creating " + threadName );
}
public void run() {
System.out.println("Running " + threadName );
try {
for(int i = 1; i <= 2; i++) {
System.out.println("Thread: " + threadName + “,第” + i + “次执行”);
// 让线程睡眠一会
Thread.sleep(50);
}
}catch (InterruptedException e) {
System.out.println(“Thread " + threadName + " interrupted.”);
}
System.out.println(“Thread " + threadName + " finish.”);
}
public void start () {
System.out.println("Starting " + threadName );
if (thread == null) {
thread = new Thread (this, threadName);
thread.start ();
}
}
public static void main(String[] args) {
RunnableDemo R1 = new RunnableDemo( “Thread-1”);
R1.start();
RunnableDemo R2 = new RunnableDemo( “Thread-2”);
R2.start();
}
}
执行结果:
给小伙伴们留一个小思考,多执行几次,你会发现执行结果不一样,为什么呢?
3.通过 Callable 和 Future 创建线程
有的时候我们可能需要让一步执行的线程在执行完成以后,提供一个返回值给到当前的主线程,主线程需要依赖这个值进行后续的逻辑处理,那么这个时候,就需要用到带返回值的线程了。创建步骤如下:
-
创建 Callable 接口的实现类,并实现 call() 方法,该 call() 方法将作为线程执行体,并且有返回值。
-
创建 Callable 实现类的实例,使用 FutureTask 类来包装 Callable 对象,该 FutureTask 对象封装了该 Callable 对象的 call() 方法的返回值。
-
使用 FutureTask 对象作为 Thread 对象的 target 创建并启动新线程。
-
调用 FutureTask 对象的 get() 方法来获得子线程执行结束后的返回值。
代码示例:
public class CallableThreadTest implements Callable {
public static void main(String[] args) {
CallableThreadTest callable = new CallableThreadTest();
FutureTask futureTask = new FutureTask<>(callable);
for(int i = 0;i < 3;i++) {
System.out.println(Thread.currentThread().getName()+" 的循环变量i的值"+i);
if(i==2) {
new Thread(futureTask,“有返回值的线程”).start();
}
}
try {
System.out.println(“子线程的返回值