Home | 簡體中文 | 繁體中文 | 雜文 | 打賞(Donations) | 雲棲社區 | OSChina 博客 | Facebook | Linkedin | 知乎專欄 | Github | Search | About

1.10. Thread 綫程

1.10.1. 實現非同步執行

		
	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();
		}	
	}				
		
		

1.10.2. 整合 Thread 類實現多綫程

		
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();
	}
}
		
		

1.10.3. 實現 Runnable 介面

		
		
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();
	}
}
		
		

1.10.4. 綫程同步

		
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();
	}
}