1.继承Thread类
源代码:
package com.zy.test.www.multiThread; /** * 多线程实现方式1:继承Thread类 * @author zy */ public class ByExtendsThread extends Thread{ public ByExtendsThread(String name) { super(name); } @Override public void run() { System.out.println(getName() + " 线程运行开始!"); for (int i = 1; i <= 5; i++) { System.out.println(getName() + " " + i); try { sleep((int) Math.random() * 10); } catch (InterruptedException e) { e.printStackTrace(); } } System.out.println(getName() + " 线程运行结束!"); } public static void main(String[] args) { System.out.println(Thread.currentThread().getName() + " 线程运行开始!"); new ByExtendsThread("A").start(); new ByExtendsThread("B").start(); System.out.println(Thread.currentThread().getName() + " 线程运行结束!"); } }
运行效果:
main 线程运行开始!
A 线程运行开始!
main 线程运行结束!
A 1
B 线程运行开始!
B 1
A 2
B 2
A 3
B 3
A 4
B 4
A 5
B 5
A 线程运行结束!
B 线程运行结束!
2.实现Runnable接口
源代码:
package com.zy.test.www.multiThread; /** * 多线程实现方式2:实现Runnable接口 * @author zy */ public class ByImplementsRunnable implements Runnable{ @Override public void run() { System.out.println(Thread.currentThread().getName() + " 线程运行开始!"); for (int i = 1; i <= 5; i++) { System.out.println(Thread.currentThread().getName() + " " + i); try { Thread.sleep((int) Math.random() * 10); } catch (InterruptedException e) { e.printStackTrace(); } } System.out.println(Thread.currentThread().getName() + " 线程运行结束!"); } public static void main(String[] args) { System.out.println(Thread.currentThread().getName() + " 线程运行开始!"); ByImplementsRunnable byImplementsRunnable = new ByImplementsRunnable(); new Thread(byImplementsRunnable, "A").start(); new Thread(byImplementsRunnable, "B").start(); System.out.println(Thread.currentThread().getName() + " 线程运行结束!");
} }
运行效果:
main 线程运行开始! A 线程运行开始! main 线程运行结束! A 1 B 线程运行开始! B 1 A 2 B 2 A 3 B 3 A 4 B 4 A 5 B 5 A 线程运行结束! B 线程运行结束!
3.使用线程池
源代码:
package com.sunny.www.multiThreads.realize; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.TimeUnit; /** * 多线程实现方式3:线程池 * @author zy */ public class ByThreadPool { private Long ticketCount; //总门票数量 public ByThreadPool(Long ticketCount) { super(); this.ticketCount = ticketCount; } public Long getTicketCount() { return ticketCount; } public void setTicketCount(Long ticketCount) { this.ticketCount = ticketCount; } public static void main(String[] args) throws InterruptedException { ByThreadPool byThreadPool = new ByThreadPool(Long.valueOf(Integer.MAX_VALUE)); int poolSize = 10; //线程池大小 ExecutorService pool = Executors.newFixedThreadPool(poolSize); long start = System.currentTimeMillis(); for(int i = 0; i < poolSize; i++){ pool.execute(new TicketWindow(byThreadPool)); } pool.shutdown(); pool.awaitTermination(Long.MAX_VALUE, TimeUnit.DAYS); long end = System.currentTimeMillis(); System.out.println("all thread complete,spend times=" + (end-start) + "ms"); } } class TicketWindow implements Runnable { private ByThreadPool byThreadPool; public TicketWindow(ByThreadPool byThreadPool) { super(); this.byThreadPool = byThreadPool; } @Override public void run() { saleTicket(); } private synchronized void saleTicket() { Long ticketCount = byThreadPool.getTicketCount(); while(0 < ticketCount){ System.out.println(Thread.currentThread().getName() + " , sale " + ticketCount--); } } }
运行效果:
1 pool-1-thread-3 , sale 100 2 pool-1-thread-1 , sale 100 3 pool-1-thread-1 , sale 99 4 pool-1-thread-1 , sale 98 5 pool-1-thread-1 , sale 97 6 pool-1-thread-1 , sale 96 7 pool-1-thread-1 , sale 95 8 。。。。