dome实现
public class TestThread extends Thread{
@Override
public void run() {
for (int i = 0; i < 20; i++) {
System.out.println("第"+(i+1)+"次打印");
}
}
public static void main(String[] args) {
TestThread testThread = new TestThread();
//自动调用线程类的run方法
testThread.start();
}
}
运行结果
dome实现
public class TestRunnable implements Runnable {
@Override
public void run() {
for (int i = 0; i < 20; i++) {
System.out.println("TestRunnable类第"+(i+1)+"次打印");
}
}
public static void main(String[] args) {
//创建业务实现类
TestRunnable testRunnable = new TestRunnable();
//创建代理线程
Thread thread = new Thread(testRunnable);
//线程启动
thread.start();
}
}
运行结果
import java.util.concurrent.Callable; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; public class TestCallable implements Callable{ @Override public Integer call() throws Exception { int i; for (i = 0; i < 20; i++) { System.out.println("TestCallable类第" + (i + 1) + "次打印"); } return i; } public static void main(String[] args) { //获取线程池服务 ExecutorService es = Executors.newCachedThreadPool(); //提交处理逻辑 es.submit(new TestCallable()); //关闭服务 es.shutdown(); } }
运行结果



