多线程
继承Thread类
//创建线程方式一:继承Thread类,重写run()方法,调用start开启线程 public class TestThread1 extends Thread{ @Override public void run() { for (int i = 0; i < 20; i++) { System.out.println("我在看代码"+i); } } public static void main(String[] args) { //main线程,主线程 //创建一个线程对象 TestThread1 testThread1 = new TestThread1(); //调用start方法开启线程 testThread1.start(); for (int i = 0; i < 20; i++) { System.out.println("我在学习多线程"+i); } } }
网图下载
工具包:commons-io-2.6.jar
import org.apache.commons.io.FileUtils; import java.io.File; import java.io.IOException; import java.net.URL; //实现多线程同步下载器 public class TestThread2 extends Thread{ private String url; private String name; public TestThread2(String url, String name){ this.name = name; this.url = url; } //线程执行体 @Override public void run() { WebDownloader webDownloader = new WebDownloader(); webDownloader.downloader(url,name); System.out.println("下载了文件名为"+name); } public static void main(String[] args) { TestThread2 testThread2 = new TestThread2("https://img-blog.csdnimg.cn/d401aef9ed0f4c9394ae8930e6686c26.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA6Lev6L655pGG5pGK,size_20,color_FFFFFF,t_70,g_se,x_16","1.jpg"); testThread2.start(); } } //下载器 class WebDownloader{ //下载方法 public void downloader(String url,String name){ try { FileUtils.copyURLToFile(new URL(url),new File(name)); } catch (IOException e) { e.printStackTrace(); System.out.println("IO异常,downloader方法出现问题"); } } }
实现runnable接口
//创建线程方式2:实现runnable接口,重写run方法,执行线程需要丢入runnable接口实现类,调用start方法 public class TestThread3 implements Runnable{ @Override public void run() { for (int i = 0; i < 20; i++) { System.out.println("我在看代码" + i); } } public static void main(String[] args) { //main线程,主线程 //创建runnable接口的实现类对象 TestThread3 testThread3 = new TestThread3(); //创建线程对象,通过线程对象来开启我们的线程,代理 Thread thread = new Thread(testThread3); thread.start(); for (int i = 0; i < 100; i++) { System.out.println("我在学习多线程"+i); } } }
则用实现runnable接口的网图下载只需要改两处:
public class TestThread2 implements Runnable{
public static void main(String[] args) { TestThread2 testThread2 = new TestThread2("https://img-blog.csdnimg.cn/d401aef9ed0f4c9394ae8930e6686c26.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA6Lev6L655pGG5pGK,size_20,color_FFFFFF,t_70,g_se,x_16","1.jpg"); new Thread(testThread2).start(); }
小结
初识并发问题
//多个线程同时操作同一个对象 //买火车票的例子 //发现问题:多个线程操作同一个资源的情况下,线程不安全,数据紊乱 public class TestThread4 implements Runnable{ private int ticketNums = 10; @Override public void run() { while (true){ if(ticketNums<=0) break; try { Thread.sleep(200);//模拟延时 } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(Thread.currentThread().getName()+"拿到了第"+ticketNums--+"张票"); } } public static void main(String[] args){ TestThread4 ticket = new TestThread4(); new Thread(ticket,"小明").start(); new Thread(ticket,"小红").start(); new Thread(ticket,"黄牛").start(); } }
龟兔赛跑
public class Race implements Runnable{ //胜利者 private static String winner; @Override public void run() { for (int i = 0; i <= 200; i++) { //模拟兔子休息 if(Thread.currentThread().getName().equals("兔子")&& i%10==0){ try { Thread.sleep(2); } catch (InterruptedException e) { e.printStackTrace(); } } //判断比赛是否结束 boolean flag = gameOver(i); //如果比赛结束了,就停止程序 if(flag){ break; } System.out.println(Thread.currentThread().getName()+"跑了"+i+"步"); } } //判断比赛是否完成 private boolean gameOver(int steps){ if(winner!=null) { return true; }{ if(steps>=200) { winner = Thread.currentThread().getName(); System.out.println("winner is" + winner); return true; } } return false; } public static void main(String[] args){ Race race = new Race(); new Thread(race,"兔子").start(); new Thread(race,"乌龟").start(); } }
Lambda表达式
静态代理模式
//静态代理模式总结: //真实对象和代理对象都要实现同一个接口 //代理对象要代理真实角色 //好处:代理对象可以做很多真实对象做不了的事情,真实对象专注做自己的事情 public class StaticProxy { public static void main(String[] args){ /*new Thread(new Runnable() { @Override public void run() { System.out.println("我爱你"); } }).start();*/ new Thread(()-> System.out.println("我爱你")).start();//Lambda表达式 //此处对比理解,Thread代理runnable对象 /*Weddingcompany weddingcompany = new Weddingcompany(new You());//把你给婚庆公司 weddingcompany.HappyMarry();*/ new Weddingcompany(new You()).HappyMarry(); } } 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() { before(); this.target.HappyMarry();//真实对象 after(); } private void after() { System.out.println("布置现场"); } private void before() { System.out.println("收尾款"); } }