1,线程状态转换
无限期等待:
限期等待:
线程生命流程:
2,实现方式
代码实现样例【三种方式】:
package com.cnblogs.mufasa.demo2; import java.util.concurrent.Callable; public class test1_Runnable implements Runnable{ @Override public void run() { for(int i=0;i<50;i++){ System.out.println("当前线程:"+i); } } } class test2_Callable implements Callable<String> { private int num; public test2_Callable(){} public test2_Callable(int num){ this.num=num; } @Override public String call() throws Exception { for(int i=0;i<50;i++){ System.out.println(this.num+"线程:"+i); } return num+"线程已完成"; } } class test3_Thread extends Thread { private int num; public test3_Thread(){} public test3_Thread(int num){ this.num=num; } @Override public void run() { for(int i=0;i<50;i++){ System.out.println(this.num+"线程:"+i); } } }
package com.cnblogs.mufasa.demo2; import java.util.concurrent.ExecutionException; import java.util.concurrent.FutureTask; public class Client { public static void main(String[] args) throws ExecutionException, InterruptedException { //实现 Runnable 接口 // test1_Runnable instance=new test1_Runnable(); // Thread thread=new Thread(instance); // Thread thread1=new Thread(instance); // thread.start(); // thread1.start(); //实现 Callable 接口 // test2_Callable instance=new test2_Callable(1); // FutureTask<String> ft=new FutureTask<>(instance); // Thread thread = new Thread(ft); // test2_Callable instance1=new test2_Callable(2); // FutureTask<String> ft2=new FutureTask<>(instance1); // Thread thread1 = new Thread(ft2); // // thread.start(); // thread1.start(); // System.out.println(ft.get()); // System.out.println(ft2.get()); //继承 Thread 类 test3_Thread thread1=new test3_Thread(1); test3_Thread thread2=new test3_Thread(2); thread1.start(); thread2.start(); } }
3,
原文地址:https://www.cnblogs.com/Mufasa/p/11430993.html
时间: 2024-11-17 11:39:45