public class ThreadDemo { public static void main(String[] args) { MyThread mt1 = new MyThread("thread1"); MyThread mt2 = new MyThread("thread2"); MyThread mt3 = new MyThread("thread3"); mt1.start(); mt2.start(); mt3.start(); } }
public class MyThread extends Thread { private String name;
public MyThread(String name) { this.name = name; }
@Override public void run() { for (int i = 0; i < 10; i++) { System.out.println("Thread start:" + this.name + ",i=" + i); } } } //输出是交替执行的,说明是多线程
public class RunnableDemo { public static void main(String[] args) { MyRunnable mr1 = new MyRunnable("runnable1"); MyRunnable mr2 = new MyRunnable("runnable2"); MyRunnable mr3 = new MyRunnable("runnable3"); Thread t1 = new Thread(mr1); Thread t2 = new Thread(mr2); Thread t3 = new Thread(mr3); t1.start(); t2.start(); t3.start(); } }
public class MyRunnable implements Runnable { private String name;
public MyRunnable(String name) { this.name = name; } @Override public void run() { for (int i = 0; i < 10; i++) { System.out.println("Thread start:" + this.name + ",i=" + i); } } } //输出是交替执行的,说明是多线程
public class MyCallable implements Callable<String> { @Override public String call() throws Exception { String val = "test"; System.out.println("ready to work"); Thread.currentThread().sleep(4000); System.out.println("Work is done"); return val; } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
方式1:通过FutureTask public class FutureTaskDemo { public static void main(String[] args) throws ExecutionException, InterruptedException { FutureTask<String> futureTask = new FutureTask<>(new MyCallable()); new Thread(futureTask).start(); if (!futureTask.isDone()) { System.out.println("Please wait"); } System.out.println("Task return:" + futureTask.get()); } }
输出: Please wait ready to work Work is done Task return:test
publicclassWaitSleepDemo{ publicstaticvoidmain(String[] args){ final Object lock = new Object(); new Thread(new Runnable() { @Override publicvoidrun(){ System.out.println("Thread A wait for lock"); synchronized (lock) { try { System.out.println("Thread A get lock"); Thread.sleep(20); System.out.println("Thread A do wait method"); lock.wait(1000); System.out.println("Thread a is done"); } catch (InterruptedException e) { e.printStackTrace(); }
new Thread(new Runnable() { @Override publicvoidrun(){ System.out.println("Thread B wait for lock"); synchronized (lock) { try { System.out.println("Thread B get lock"); System.out.println("Thread B is sleeping"); Thread.sleep(10); System.out.println("Thread B is done"); } catch (InterruptedException e) { e.printStackTrace(); }
}
} }).start(); } }
输出: Thread A wait for lock Thread A get lock Thread B wait for lock Thread A do wait method Thread B get lock Thread B is sleeping Thread B is done Thread A is done
publicclassWaitSleepDemo{ publicstaticvoidmain(String[] args){ final Object lock = new Object(); new Thread(new Runnable() { @Override publicvoidrun(){ System.out.println("Thread A wait for lock"); synchronized (lock) { try { System.out.println("Thread A get lock"); Thread.sleep(20); System.out.println("Thread A do wait method"); lock.wait(); System.out.println("Thread A is done"); } catch (InterruptedException e) { e.printStackTrace(); }
new Thread(new Runnable() { @Override publicvoidrun(){ System.out.println("Thread B wait for lock"); synchronized (lock) { try { System.out.println("Thread B get lock"); System.out.println("Thread B is sleeping"); Thread.sleep(10); System.out.println("Thread B is done"); lock.notify(); } catch (InterruptedException e) { e.printStackTrace(); }
}
} }).start(); } } 输出: Thread A wait for lock Thread A get lock Thread B wait for lock Thread A do wait method Thread B get lock Thread B is sleeping Thread B is done //notify通知A Thread A is done