电脑cpu网卡 | 笔记本主板网卡
639 2023-04-03 04:08:12
//创建线程方式一:继承Thread类,重写run()方法,调用start开启线程//总结:注意:线程开启不一定立即执行,由cpu调度执行public class TestThread1 extends Thread{ @Override public void run() { //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); } }}====================================================================import org.apache.commons.io.FileUtils;import java.io.File;import java.io.IOException;import java.net.URL;//练习Thread,实现多线程同步下载图片public class TestThread2 extends Thread{//使用实现Runnable接口的方法进行操作//public class TestThread2 implements Runnable{ private String url;//网络图片地址 private String name;//保存的文件名 public TestThread2(String url,String name){ this.url = url; this.name = name; } //下载图片线程的执行体 @Override public void run() { WebDownloader webDownloader = new WebDownloader(); webDownloader.downloader(url,name); System.out.println("下载了文件名为:"+name); } public static void main(String[] args) { TestThread2 t1 = new TestThread2("/d/know/2023030410/yqmomo02lzg.webp","1.webp"); TestThread2 t2 = new TestThread2("/d/know/2023030410/4uhay02l2is.webp","2.webp"); TestThread2 t3 = new TestThread2("/d/know/2023030410/l3ckh5zrmb3.webp","3.webp"); //t1,t2,t3同时执行 t1.start(); t2.start(); t3.start(); //使用实现Runnable接口的方法进行操作 /*new Thread(t1).start(); new Thread(t2).start(); new Thread(t3).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方法出现问题"); } }}
//创建线程方式2:实现runnabl接口,重写run方法,执行线程需要丢入runnab接口实现类,调用start方法public class TestThread3 implements Runnable{ @Override public void run() { //run方法线程体 for (int i = 0; i < 200; i++) { System.out.println("我在看代码---"+i); } } public static void main(String[] args) { //创建runnable接口的实现类对象 TestThread3 testThread3 = new TestThread3(); //创建线程对象,通过线程对象来开启我们的线程,代理 /*Thread thread = new Thread(testThread3); thread.start();*/ new Thread(testThread3).start(); for (int i = 0; i < 1000; i++) { System.out.println("我在学习多线程---"+i); } }}
实现Runnable具有多线程能力
启动线程:传入目标对象+Thread对象.start()
推荐使用:避免单继承局限性,灵活方便,方便同一个对象被多个线程使用
//一份资源StartThread4 station = new StartThread4();//多个代理new Thread(station,"小明");new Thread(station,"老师");new Thread(station,"小红");
//多个线程同时操作同一个对象//买火车票的例子//发现问题:多个线程操作同一个资源的情况下,线程不安全,数据紊乱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 testThread4 = new TestThread4(); new Thread(testThread4,"小明").start(); new Thread(testThread4,"老师").start(); new Thread(testThread4,"黄牛党").start(); }}
public class Race implements Runnable{ //胜利者 private static String winner; @Override public void run() { for (int i = 0; i <= 100; i++) { //模拟兔子休息 /*if (Thread.currentThread().getName().equals("兔子") && i%10==0){ try { Thread.sleep(5); } 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; }else { if (steps>=100){ 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(); }}
import org.apache.commons.io.FileUtils;import java.io.File;import java.io.IOException;import java.net.URL;import java.util.concurrent.*;//线程创建方式三:实现callable接口public class TestCallable implements Callable<Boolean> { private String url;//网络图片地址 private String name;//保存的文件名 public TestCallable(String url,String name){ this.url = url; this.name = name; } //下载图片线程的执行体 @Override public Boolean call() throws Exception { WebDownloader webDownloader = new WebDownloader(); webDownloader.downloader(url,name); System.out.println("下载了文件名为:"+name); return true; } public static void main(String[] args) throws ExecutionException, InterruptedException { TestCallable t1 = new TestCallable("/d/know/2023030410/yqmomo02lzg.webp","1.webp"); TestCallable t2 = new TestCallable("/d/know/2023030410/4uhay02l2is.webp","2.webp"); TestCallable t3 = new TestCallable("/d/know/2023030410/l3ckh5zrmb3.webp","3.webp"); //创建执行服务: ExecutorService ser = Executors.newFixedThreadPool(3); //提交执行 Future<Boolean> r1 = ser.submit(t1); Future<Boolean> r2 = ser.submit(t2); Future<Boolean> r3 = ser.submit(t3); //获取结果 boolean rs1 = r1.get(); boolean rs2 = r2.get(); boolean rs3 = r3.get(); System.out.println(rs1); System.out.println(rs2); System.out.println(rs3); //关闭服务 ser.shutdownNow(); }}//下载器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方法出现问题"); } }}
//静态代理模式总结://真实对象和代理对象都要实现同一个接口//代理对象要代理真实角色//好处: //代理对象可以做很多真实对象做不了的事情 //真实对象专注做自己的事情public class StaticProxy { public static void main(String[] args) { //多线程的实现方式,使用lambda表达式 /*new Thread( ()-> System.out.println("我爱你") ).start(); new WeddingCompany(new You()).HappyMarry();*/ //原来使用you调用HappyMarry()方法,现在把you作为参数传入WeddingCompany,使用weddingCompany调用HappyMarry方法 /*You you = new You();//你要结婚 you.HappyMarry();*/ WeddingCompany weddingCompany = new WeddingCompany(new You()); weddingCompany.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("结婚之前,布置现场"); }}