注意:线程不一定立即执行,CPU安排调度
package com.cnblogs.thread; //创建线程方式一:继承Thread类,重写run()方法,调用start开启线程 public class TestThread extends Thread{ @Override public void run() { for (int i = 0; i < 100; i++) { System.out.println("看代码中。。。"); } } /* 线程开启不一定立即执行,由cpu调度执行 */ public static void main(String[] args) { //创建一个线程对象 TestThread testThread1 = new TestThread(); testThread1.start(); //两个线程交替执行 for (int i = 0; i < 1000; i++) { System.out.println("俺在学习多线程!!!"); } } }
推荐使用Runnable对象,因为Java单继承的局限性
package com.cnblogs.thread; //创建线程方式二:实现runnable接口,重写run方法,执行线程需要丢入runnable接口实现类,调用start方法 public class TestThread3 implements Runnable{ @Override public void run() { for (int i = 0; i < 100; i++) { System.out.println("看代码中。。。"); } } public static void main(String[] args) { //创建runnable接口的实现类对象 TestThread3 testThread3 = new TestThread3(); //创建线程对象,通过线程对象来开启我们的线程,代理 // Thread thread = new Thread(); // thread.start(); //简写 new Thread(testThread3).start(); for (int i = 0; i < 1000; i++) { System.out.println("俺在学习多线程!!!"); } } }
继承Thread类
继承Runnable接口
package com.cnblogs.thread; //模拟龟兔赛跑 public class Race implements Runnable{ private static String winner; @Override public void run() { for (int i = 0; i <= 1000; 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 step){ if(winner != null){ return true; }else{ if(step >= 1000){ 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(); } }
package com.cnblogs.thread; import org.apache.commons.io.FileUtils; import java.io.File; import java.io.IOException; import java.net.URL; /* 练习Thread 实现多线程同步下载图片 */ public class TestThread2 extends Thread{ 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("https://www.www.zyiz.net/i/ll/?i=2021051913555284.png?,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80Mzg4NDIzNA==,size_16,color_FFFFFF,t_70","1.jpg"); TestThread2 t2 = new TestThread2("https://www.www.zyiz.net/i/ll/?i=20210519135426133.png","2.jpg"); TestThread2 t3 = new TestThread2("https://www.www.zyiz.net/i/ll/?i=20210519135102589.png?,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80Mzg4NDIzNA==,size_16,color_FFFFFF,t_70","3.jpg"); t1.start(); t2.start(); t3.start(); /* 下载了文件名为:2.jpg的文件 下载了文件名为:3.jpg的文件 下载了文件名为:1.jpg的文件 */ } } class WebDownLoader{ public void downloader(String url , String name){ try { FileUtils.copyURLToFile(new URL(url),new File(name)); } catch (IOException e) { System.out.println("IO异常,downloader方法出现问题!"); e.printStackTrace(); } } }
ExecutorService:用来存储线程的池子,把新建线程/启动线程/关闭线程的任务都交给池来管理
Executors 辅助创建线程池的工具类
package com.cnblogs; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; public class TestRunnable { public static void main(String[] args) { MyRunnable target = new MyRunnable(); //创建线程池 ExecutorService pool = Executors.newFixedThreadPool(5); for (int i = 0; i < 5; i++) { pool.execute(target); } } } class MyRunnable implements Runnable{ @Override public void run() { for (int i = 0; i < 10; i++) { System.out.println("多线程哦!!" + Thread.currentThread().getName()); } } }