Creación de subprocesos múltiples (2)
-
Método 2 de implementación de creación de subprocesos :
- Implementar la interfaz ejecutable,
- Reescribe el método de ejecución,
- El subproceso de ejecución debe lanzarse a la clase de implementación de la interfaz ejecutable y llamar al método de inicio.
//创建线程方式2:实现runnable接口,重写run方法,执行线程需要丢入runnable接口实现类,调用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(); // thread.start(); //上面两句写成一句 new Thread(testThread3).start(); for (int i = 0; i < 1000; i++) { System.out.println("我在学习多线程--"+i); } } }
-
La diferencia entre las dos formas de creación de hilos
- Heredar la clase Thread
- Las subclases heredan la clase Thread con capacidades de subprocesos múltiples
- Subproceso de inicio: subclase object.start ();
- No recomendado: evitar las limitaciones de la herencia única OOP
- Implementar la interfaz Runnable
- Implementar interfaz Ejecutable con capacidad de subprocesos múltiples
- Iniciar hilo: pasar el objeto de destino + hilo objeto.start ();
- Uso recomendado: evite las limitaciones de la herencia única, flexible y conveniente, y conveniente para que varios subprocesos utilicen el mismo objeto
- Heredar la clase Thread
-
Problema de concurrencia inicial: un ejemplo de compra de un billete de tren
/多个线程同时操作同一个对象 //买火车票的例子 //发现问题:多个线程操作同一个资源的情况下,线程不安全了,数据紊乱 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(); } //Thread.currentThread().getName() 得到线程的名字 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(); } }
-
Ejemplo de práctica: la tortuga y la liebre
//模拟龟兔赛跑
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(10);
} 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>=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();
}
}
-
Método de creación de hilo tres:
- Para implementar la interfaz invocable, debe devolver el tipo de valor devuelto
- Reescribir el método de llamada, es necesario que se agote la excepción
- Crea una audiencia objetivo
- Cree un servicio de ejecución: ExecutorService ser = Executors.newFixedThreadPool (3);
- Enviar para ejecución: Future r1 = ser.submit (t1);
- Obtenga el resultado: boolean rs1 = r1.get ();
- Cierre el servicio: ser.shutdownNow ();
import org.apache.commons.io.FileUtils; import java.io.File; import java.io.IOException; import java.net.URL; import java.util.concurrent.*; //线程方式三:实现callable接口 /** callable的好处 1.可以定义返回值 2.可以跑出异常 */ 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() { 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("https://img-blog.csdnimg.cn/20210316213155604.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L1h1bl9pbmRlcGVuZGVudA==,size_16,color_FFFFFF,t_70#pic_center","1.jpg"); TestCallable t2=new TestCallable("https://img-blog.csdnimg.cn/20210316213209625.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L1h1bl9pbmRlcGVuZGVudA==,size_16,color_FFFFFF,t_70#pic_center","2.jpg"); TestCallable t3=new TestCallable("https://img-blog.csdnimg.cn/20210316213228816.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L1h1bl9pbmRlcGVuZGVudA==,size_16,color_FFFFFF,t_70#pic_center","3.jpg"); //创建执行服务: 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方法出现问题"); } } }