本文节选自电子书《Netkiller Java 手札》
1.10. Thread 线程
public void testThread() throws Exception { try { Thread sendmail = new Thread(new Runnable() { @Override public void run() { // Sendmail log.info("Sendmail OK"); } }); sendmail.setName("sendmail"); sendmail.start(); } catch (Exception e) { e.printStackTrace(); } }
package cn.netkiller.ipo.test; public class MyThread extends Thread { private String name; public MyThread(String name) { super(); this.name = name; } public void run() { for (int i = 0; i < 10; i++) { System.out.println("Thread:" + this.name + ",i=" + i); } } public static void main(String[] args) { MyThread mt1 = new MyThread("A"); MyThread mt2 = new MyThread("B"); mt1.start(); mt2.start(); } }
package cn.netkiller.ipo.test; 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:" + this.name + ",i=" + i); } } public static void main(String[] args) { MyRunnable mr1 = new MyRunnable("A"); MyRunnable mr2 = new MyRunnable("B"); new Thread(mr1).start(); new Thread(mr2).start(); new Thread(new MyRunnable("C")).start(); } }
package cn.netkiller.thread; public class SynchronizedThread extends Thread { private int count = 0; @Override public /*synchronized*/ void run() { count++; System.out.println(Thread.currentThread().getName() + " count:" + count); } public static void main(String[] args) { SynchronizedThread myThread = new SynchronizedThread(); Thread thread1 = new Thread(myThread, "thread1"); Thread thread2 = new Thread(myThread, "thread2"); Thread thread3 = new Thread(myThread, "thread3"); Thread thread4 = new Thread(myThread, "thread4"); Thread thread5 = new Thread(myThread, "thread5"); thread1.start(); thread2.start(); thread3.start(); thread4.start(); thread5.start(); } }
线程运行不分先后
thread2 count:3 thread4 count:4 thread1 count:3 thread3 count:3 thread5 count:5
package cn.netkiller.thread; public class SynchronizedThread extends Thread { private int count = 0; @Override public synchronized void run() { count++; System.out.println(Thread.currentThread().getName() + " count:" + count); } public static void main(String[] args) { SynchronizedThread myThread = new SynchronizedThread(); Thread thread1 = new Thread(myThread, "thread1"); Thread thread2 = new Thread(myThread, "thread2"); Thread thread3 = new Thread(myThread, "thread3"); Thread thread4 = new Thread(myThread, "thread4"); Thread thread5 = new Thread(myThread, "thread5"); thread1.start(); thread2.start(); thread3.start(); thread4.start(); thread5.start(); } }
thread1 count:1 thread5 count:2 thread4 count:3 thread2 count:4 thread3 count:5
package cn.netkiller.thread; public class MultiThread { private static int count = 0; public synchronized void add() { count++; System.out.println(Thread.currentThread().getName() + " count:" + count); } public static void main(String[] args) throws InterruptedException { final MultiThread multiThread1 = new MultiThread(); final MultiThread multiThread2 = new MultiThread(); final MultiThread multiThread3 = new MultiThread(); final MultiThread multiThread4 = new MultiThread(); final MultiThread multiThread5 = new MultiThread(); new Thread(new Runnable() { public void run() { multiThread1.add(); } }).start(); new Thread(new Runnable() { public void run() { multiThread2.add(); } }).start(); new Thread(new Runnable() { public void run() { multiThread3.add(); } }).start(); new Thread(new Runnable() { public void run() { multiThread4.add(); } }).start(); new Thread(new Runnable() { public void run() { multiThread5.add(); } }).start(); } }