时间:2023-01-29 10:46:21 | 栏目:JAVA代码 | 点击:次
public class FirstThreadTest extends Thread { int i = 0; // 重写run方法,run方法的方法体就是现场执行体 public void run() { for (; i < 5; i++) { System.out.println(getName() + " " + i); } } public static void main(String[] args) { for (int i = 0; i < 3; i++) { System.out.println(Thread.currentThread().getName() + " : " + i); if (i == 2) { new FirstThreadTest().start(); new FirstThreadTest().start(); } } } }
上述代码中Thread.currentThread()方法返回当前正在执行的线程对象。GetName()方法返回调用该方法的线程的名字。
public class RunnableThreadTest implements Runnable { private int i; public void run() { for (i = 0; i < 5; i++) { System.out.println(Thread.currentThread().getName() + " " + i); } } public static void main(String[] args) { for (int i = 0; i < 3; i++) { System.out.println(Thread.currentThread().getName() + " " + i); if (i == 2) { RunnableThreadTest rtt = new RunnableThreadTest(); new Thread(rtt, "新线程1").start(); new Thread(rtt, "新线程2").start(); } } } }
线程的执行流程很简单,当执行代码start()时,就会执行对象中重写的void run();方法,该方法执行完成后,线程就消亡了。
public class RunnableThreadTest { // 目的是为了代码的重用【静态方法】 public static void threadRunCode_Static() { for (int i = 0; i < 5; i++) { System.out.println(Thread.currentThread().getName() + " " + i); } } // 目的是为了代码的重用【非静态方法】 public void threadRunCode() { for (int i = 0; i < 5; i++) { System.out.println(Thread.currentThread().getName() + " " + i); } } @Test public void testStatic() { // 重用静态方法中的代码【使用方法引用】 for (int i = 0; i < 3; i++) { System.out.println(Thread.currentThread().getName() + " " + i); if (i == 2) { new Thread(RunnableThreadTest::threadRunCode_Static, "线程1").start(); ; new Thread(RunnableThreadTest::threadRunCode_Static, "线程2").start(); ; } } } @Test public void testNoStatic() { // 重用非静态方法中的代码【使用方法引用】 RunnableThreadTest temp = new RunnableThreadTest(); for (int i = 0; i < 3; i++) { System.out.println(Thread.currentThread().getName() + " " + i); if (i == 2) { new Thread(temp::threadRunCode, "线程1").start(); new Thread(temp::threadRunCode, "线程2").start(); } } } @Test public void testLambda() { // 重用静态方法中的代码【使用方法引用】 for (int i = 0; i < 3; i++) { System.out.println(Thread.currentThread().getName() + " " + i); if (i == 2) { new Thread(() -> { for (int b = 0; b < 5; b++) { System.out.println(Thread.currentThread().getName() + " " + b); } },"线程1").start(); new Thread(() -> { for (int b = 0; b < 5; b++) { System.out.println(Thread.currentThread().getName() + " " + b); } },"线程2").start(); } } } }
public interface Callable{ V call() throws Exception; }
public class CallableThreadTest implements Callable<Integer> { @Override public Integer call() throws Exception { int i = 0; for (; i < 5; i++) { System.out.println(Thread.currentThread().getName() + " " + i); } return i; } public static void main(String[] args) { CallableThreadTest ctt = new CallableThreadTest(); FutureTask<Integer> ft = new FutureTask<>(ctt); for (int i = 0; i < 3; i++) { System.out.println(Thread.currentThread().getName() + " 的循环变量i的值" + i); if (i == 2) { new Thread(ft, "有返回值的线程").start(); } } try { System.out.println("子线程的返回值:" + ft.get()); } catch (InterruptedException | ExecutionException e) { e.printStackTrace(); } } }
public class CallableThreadTest { public static void main(String[] args) { FutureTask<Integer> ft = new FutureTask<>(() -> { int i = 0; for (; i < 5; i++) { System.out.println(Thread.currentThread().getName() + " " + i); } return i; }); for (int i = 0; i < 3; i++) { System.out.println(Thread.currentThread().getName() + " 的循环变量i的值" + i); if (i == 2) { new Thread(ft, "有返回值的线程").start(); } } try { System.out.println("子线程的返回值:" + ft.get()); } catch (InterruptedException | ExecutionException e) { e.printStackTrace(); } } }
优势:
劣势:
优势:
劣势:
拓展:
Lambda表达式的强大之处就是传递代码,而Runnable和Callable接口都是符合Lambda要求的函数式接口。因此,我们可以不用创建这两个接口的实现类,而是直接将其中的实现代码传递到Thread的target即可。