Java多线程
三种多线程实现方式:
-
继承Thread类,重写run方法,启动start
-
实现Runnable接口,重写run方法,创建Thread代理类并传入,启动start
-
实现Callable接口,重写call方法,创建线程池并传入submit执行。
Thread类
继承Thread类,重写run方法(返回值void,无法抛出异常),调用start方法启动线程。
一边听歌一边敲代码:
public class StartThread extends Thread { /** * 线程入口点 */ @Override public void run() { for (int i = 0; i < 20; i++) { System.out.println("一边听歌"); try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } } } public static void main(String[] args) throws InterruptedException { //创建Thread子类对象 StartThread st = new StartThread(); //启动 st.start(); for (int i = 0; i < 20; i++) { System.out.println("一边敲代码"); Thread.sleep(100); } } }
Runnable接口
实现Runnable接口,重写run方法(返回值void,无法抛出异常),创建Thread代理类,调用代理类的start方法启动线程。
同时下载多个图片:
public class IDownloader implements Runnable { private String url;//图片地址 private String name;//图片名称 public IDownloader(String url, String name) { this.url = url; this.name = name; } @Override public void run() { WebDownloader wd = new WebDownloader(); wd.download(url, name); } public static void main(String[] args) { IDownloader td1 = new IDownloader("url", "夕阳.jpg"); IDownloader td2 = new IDownloader("url", "海底.jpg"); IDownloader td3 = new IDownloader("url", "公园.jpg"); //创建三个代理类 Thread t1=new Thread(td1); Thread t2=new Thread(td2); Thread t3=new Thread(td3); //启动三个线程 t1.start(); t2.start(); t3.start(); } }
Callable接口
实现callable接口,重写call方法(有返回值,可抛出异常),创建执行服务,提交执行,获取结果,关闭服务。
模拟龟兔赛跑:
public class CRacer implements Callable<Integer> { //胜利者 private String winner; @Override public Integer call() throws InterruptedException { String threadName = Thread.currentThread().getName(); for (int i = 0; i <= 100; i++) { if (threadName.equals("pool-1-thread-1")) { //兔子跑得更快 i++; //兔子喜欢睡觉 if (i > 95) { Thread.sleep(1); } } System.out.println(threadName + "-->" + i); //比赛是否结束 boolean flag = gameOver(i, threadName); if (flag) { return i; } } return null; } private boolean gameOver(int i, String name) { if (winner != null) { return true; } else { if (i >= 100) { winner = name; System.out.println("winner -->" + name); return true; } } return false; } public static void main(String[] args) throws ExecutionException, InterruptedException { CRacer racer = new CRacer(); //创建执行服务 ExecutorService ser = Executors.newFixedThreadPool(3); //提交执行 Future<Integer> f1 = ser.submit(racer); Future<Integer> f2 = ser.submit(racer); //获取结果 Integer r1 = f1.get(); Integer r2 = f2.get(); System.out.println(r1 + "-->" + r2); //关闭服务 ser.shutdownNow(); } }
StaticProxy静态代理
接口:
-
真实角色(婚礼主人公)
-
代理角色(婚庆公司)
婚庆公司代理婚礼:
public class StaticProxy { public static void main(String[] args) { new WeddingCompany(new You()).happyMarry(); //new Thread(线程对象).start(); } } //结婚接口 interface Marry { void happyMarry(); } //真实角色 class You implements Marry { @Override public void happyMarry() { System.out.println("你和嫦娥终于奔月了..."); } } //代理角色(婚礼公司) class WeddingCompany implements Marry { //真实角色 private Marry target; public WeddingCompany(Marry target) { this.target = target; } @Override public void happyMarry() { ready(); this.target.happyMarry(); after(); } private void ready() { System.out.println("布置猪窝..."); } private void after() { System.out.println("闹玉兔..."); } }