Runnable.java(实现Runnable接口)
package com.jhy.www.thread;
//通过实现 Runnable 接口来创建线程
public class RunnableDemo implements Runnable {
private Thread t;
private String threadName;
public RunnableDemo(String name){
threadName = name;
System.out.println("创建 "+threadName);
}
@Override
public void run() {
System.out.println("运行 "+threadName);
try {
for(int i=4; i>0; i--){
System.out.println("Thread: "+threadName+","+i);
//让线程睡眠一会
Thread.sleep(50);
}
} catch (InterruptedException e) {
//e.printStackTrace();
System.out.println("Thread "+threadName+"interrupted####"+e.getMessage());
}
System.out.println("Thread "+threadName+"exiting");
}
public void start(){
System.out.println("Starting "+threadName);
if(t == null){
t=new Thread(this,threadName);
t.start();
}
}
}
--------------------------------------------------------------------------------
ThreadDemo.java(继承Thread类)
package com.jhy.www.thread;
//通过继承Thread来创建线程
public class ThreadDemo extends Thread {
private Thread t;
private String threadName;
public ThreadDemo(String name){
threadName = name;
System.out.println("创建 "+threadName);
}
@Override
public void run() {
System.out.println("运行 "+threadName);
try {
for(int i=4;i>0;i--){
System.out.println("线程"+threadName+i);
Thread.sleep(100);
}
} catch (InterruptedException e) {
//e.printStackTrace();
System.out.println("线程"+threadName+"interruped####"+e.getMessage());
}
System.out.println("线程"+threadName+"exiting");
}
@Override
public void start(){
System.out.println("开始 "+threadName);
if(t==null){
t=new Thread(this,threadName);
t.start();
}
}
}
--------------------------------------------------
CallableDemo.java(实现Callable接口)
package com.jhy.www.thread; import java.util.Random; import java.util.concurrent.Callable; //实现Callable接口可以允许我们的线程返回值或抛出异常 public class CallableDemo implements Callable { private String name; public void setName(String name){ this.name = name; } @Override public Integer call() throws Exception { //创建一个0-100的随机数 int speed = new Random().nextInt(100); int distance = 0; int i=0; while(i<100){ Thread.sleep(40); distance = i*speed; System.out.println(this.name+"跑步"+distance+"米,速度:"+speed+"m/s"); i++; } return distance; } }
----------------------------------------------------------------
测试类 TestThread.java
package com.jhy.www.test; import com.jhy.www.thread.CallableDemo; import com.jhy.www.thread.RunnableDemo; import com.jhy.www.thread.ThreadDemo; import java.util.concurrent.*; public class TestThreads { //测试多线程 public static void main(String[] args) throws ExecutionException, InterruptedException { // RunnableDemo R1 = new RunnableDemo("Thread-1"); // R1.start(); // // RunnableDemo R2 = new RunnableDemo("Thread-2"); // R2.start(); /* * 创建 Thread-1 Starting Thread-1 创建 Thread-2 Starting Thread-2 运行 Thread-1 Thread: Thread-1,4 运行 Thread-2 Thread: Thread-2,4 Thread: Thread-1,3 Thread: Thread-2,3 Thread: Thread-1,2 Thread: Thread-2,2 Thread: Thread-1,1 Thread: Thread-2,1 Thread Thread-1exiting Thread Thread-2exiting * */ // ThreadDemo T1 = new ThreadDemo("Thread-3"); // T1.start(); // ThreadDemo T2 = new ThreadDemo("Thread-4"); // T2.start(); //创建一个线程池。里面天生有3个“空”线程。Executors是调度器,对线程池进行管理 ExecutorService executorService = Executors.newFixedThreadPool(3); CallableDemo sunwu = new CallableDemo(); sunwu.setName("孙武"); CallableDemo zhaosi= new CallableDemo(); zhaosi.setName("赵四"); CallableDemo wangqi=new CallableDemo(); wangqi.setName("王七"); Future<Integer> result1 = executorService.submit(sunwu); Future<Integer> result2 = executorService.submit(zhaosi); Future<Integer> result3 = executorService.submit(wangqi); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } executorService.shutdown(); //关闭线程池释放所有资源
System.out.println("####孙武跑了"+result1.get()+"米"); System.out.println("####赵四跑了"+result2.get()+"米"); System.out.println("####王七跑了"+result3.get()+"米"); } }
附一张图:
备注:还可以参考
http://www.runoob.com/java/java-multithreading.html