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(); } }
原文出处:Netkiller 系列 手札
本文作者:陈景峯
转载请与作者联系,同时请务必标明文章原始出处和作者信息及本声明。
时间: 2024-11-13 06:35:09