package com.thread; public class FirstThreadTest extends Thread{ int i = 0; //重写run方法,run方法的方法体就是现场执行体 public void run() { for(;i<100;i ){ System.out.println(getName() " " i); } } public static void main(String[] args) { for(int i = 0;i< 100;i ) { System.out.println(Thread.currentThread().getName() " : " i); if(i==20) { new FirstThreadTest().start(); new FirstThreadTest().start(); } } } }
package com.thread; public class RunnableThreadTest implements Runnable { private int i; public void run() { for(i = 0;i <100;i ) { System.out.println(Thread.currentThread().getName() " " i); } } public static void main(String[] args) { for(int i = 0;i < 100;i ) { System.out.println(Thread.currentThread().getName() " " i); if(i==20) { RunnableThreadTest rtt = new RunnableThreadTest(); new Thread(rtt,"新线程1").start(); new Thread(rtt,"新线程2").start(); } } } }
public interface Callable { V call() throws Exception; }
package com.thread; import java.util.concurrent.Callable; import java.util.concurrent.ExecutionException; import java.util.concurrent.FutureTask; public class CallableThreadTest implements Callable<Integer> { public static void main(String[] args) { CallableThreadTest ctt = new CallableThreadTest(); FutureTask<Integer> ft = new FutureTask<>(ctt); for(int i = 0;i < 100;i ) { System.out.println(Thread.currentThread().getName() " 的循环变量i的值" i); if(i==20) { new Thread(ft,"有返回值的线程").start(); } } try { System.out.println("子线程的返回值:" ft.get()); } catch (InterruptedException e) { e.printStackTrace(); } catch (ExecutionException e) { e.printStackTrace(); } } @Override public Integer call() throws Exception { int i = 0; for(;i<100;i ) { System.out.println(Thread.currentThread().getName() " " i); } return i; } }