上家公司中觉得没有成长,作为一名程序员,果断12月份辞职了,发现自己真的很多短板,许多技术全部忘记,面试中有提到线程,忘得差不多特此将这些做一下记录;
面试中提问:1.线程的几种创建方式
1.继承thead类,缺点是类与类之间是单继承,占用了继承名额
2.实现runable接口,可以对多个线程操作同一个对象
3.实现callable接口,通过创建执行服务完成
4.通过线程池创建,可以提高响应速度,提高资源利用,便于线程管理
既然问道这个问题,我便希望把线程知识全部过一遍;
1.通过继承Thread类
import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; public class ZCThead extends Thread{ /*需下载文件名称*/ private String name; /*下载文件路径*/ private String url; /*弊端:只能进行单继承,占用继承名额*/ @Override public void run(){ /*下载文件*/ WebDownLoader web=new WebDownLoader(); web.downloader(url,name); System.out.println("Thread启动"); } public ZCThead(String name,String url){ this.url=url; this.name=name; } public ZCThead(){} public static void main(String[] args) { ZCThead thead=new ZCThead("1.txt","C:\\Users\\17290\\Desktop\\1.txt"); thead.start(); } } class WebDownLoader { public void downloader(String url,String name){ /*文件输入流读取文件*/ File file=new File(url); File file2=new File("D:\\test\\copy\\"+name); try( FileInputStream fileInputStream =new FileInputStream(file); FileOutputStream fileOutputStream=new FileOutputStream(file2,false); ) { byte [] bytes =new byte[1024]; int len =-1;//表示每次读取的字节长度 //把数据读取到字节数组中,并且返回读取的字节数,若字节数为-1,则表示已经读完 while((len=fileInputStream.read(bytes))!=-1){ fileOutputStream.write(bytes); //stringBuffer.append(new String()); } //关闭流 // fileInputStream.close(); fileOutputStream.close(); System.out.println(name+"复制完成"); } catch (IOException e) { e.printStackTrace(); } } }
2. 通过实现runable接口
public class ZCinterface implements Runnable{ private int CarNum=20; public static void main(String[] args) { Thread thread=new Thread(new ZCinterface()); new Thread(thread,"小明").start(); new Thread(thread,"小王").start(); new Thread(thread,"小张").start(); } @Override public void run(){ System.out.println("1"); while (true){ if (CarNum<=0){ break; } try { Thread.sleep(200); System.out.println(Thread.currentThread().getName()+"购买了第"+CarNum--+"号"); } catch (InterruptedException e) { throw new RuntimeException(e); } } } }
3.实现callable接口
import java.util.concurrent.*; public class ZCCallable implements Callable<String> { @Override public String call() throws Exception { System.out.println("执行call"); return "执行call"; } public static void main(String[] args) throws ExecutionException, InterruptedException { ZCCallable zcCallable=new ZCCallable(); /*创建服务器*/ ExecutorService executorService= Executors.newFixedThreadPool(1); /*提交执行*/ Future<String> re1=executorService.submit(zcCallable); /*获取结果*/ String re=re1.get(); /*关闭服务*/ executorService.shutdownNow(); } }
3.通过线程池创建
import java.util.concurrent.*; public class ZCRunable implements Runable { @Override public String run() { System.out.println(Thread.currentThread().getName()); return "执行call"; }/*提高响应速度(减少创建新线程的时间),降低资源占用(线程重复利用),方便管理线程 * 1.设置最大线程数 * 2.设置最大超时时间 * 3.核心池的大小等 * */ public static void main(String[] args) { ZCRunable zcRunable=new ZCRunable(); /*创建服务器*/ ExecutorService executorService= Executors.newFixedThreadPool(1); /*提交执行*/ executorService.execute(new Thread(zcRunable)); /*关闭服务*/ executorService.shutdownNow(); } }