多线程
继承Thread
下载案例
public class TestThread extends Thread{ private String url; private String name; public TestThread(String url, String name) { this.url = url; this.name = name; } @Override public void run() { WebDownLoader webDownLoader=new WebDownLoader(); webDownLoader.download(url,name); System.out.println("下载了文件名为:"+name); } public static void main(String[] args) { TestThread testThread=new TestThread("https://csdnimg.cn/release/blogv2/dist/pc/img/original.png","1.png"); TestThread testThread1 = new TestThread("https://so1.360tres.com/t010f9f451920831475.png","2.png"); testThread.start(); testThread1.start(); } }
创建下载器
public class WebDownLoader { public void download(String url,String name){ try { FileUtils.copyURLToFile(new URL(url),new File(name)); } catch (IOException e) { e.printStackTrace(); System.out.println("io异常,download方法出现问题!"); } } }
2 实现runnable接口
尽量使用这种,灵活多用。
并发问题
3.实现callable
可以定义返回值
可以抛出
1.创建服务
2.提交执行
3.获取结果
4.关闭服务
静态代理
真实对象和代理对象要实现同一个接口。
public class StaticProxy { public static void main(String[] args) { weddingMarrry weddingMarrry=new weddingMarrry(new you()); weddingMarrry.HappyMarry(); } } interface Marry{ void HappyMarry(); } //真实角色 class you implements Marry{ @Override public void HappyMarry() { System.out.println("结婚"); } } //代理角色,帮助你结婚 class weddingMarrry implements Marry{ private Marry marryTarget; public weddingMarrry(Marry marryTarget) { this.marryTarget = marryTarget; } @Override public void HappyMarry(){ this.marryTarget.HappyMarry(); } }
函数式接口:
任何接口,如果只包含唯一一个抽象方法,那么他就是一个函数式接口
对于函数式接口,我们可以通过lambda表达式来创建该接口的对象
线程
线程停止
import com.sun.corba.se.impl.resolver.BootstrapResolverImpl; //线程停止 不建议使用stop或者destory 或者jdk不建议使用的方法 //建议设置一个标志位 public class TestStop implements Runnable{ //1.设置一个标志位 private boolean flag=true; @Override public void run() { int i=0; while (flag){ System.out.println("run..."+i++); } } public void stop(){ this.flag=false; } //main方法 public static void main(String[] args) { TestStop testStop = new TestStop(); new Thread(testStop).start(); for (int i = 0; i < 1000; i++) { System.out.println("main"+i); if (i==900){ testStop.stop(); System.out.println("线程....停止了"); } } } }
模拟倒计时和计时器
public class TestSleep02 { //模拟倒计时 public static void stop() throws InterruptedException { int num=10; while (true){ Thread.sleep(1000); System.out.println(num--); if (num<=0){ break; } } } public static void main(String[] args) throws InterruptedException { Date date=new Date(System.currentTimeMillis()); while (true){ Thread.sleep(1000); System.out.println(new SimpleDateFormat("HH:mm:ss").format(date)); date=new Date(System.currentTimeMillis()); } } }
线程礼让
礼让不一定成功,要看cpu的心情
线程状态
public class TestState { public static void main(String[] args) throws InterruptedException { Thread thread= new Thread(()->{ for (int i = 0; i < 5; i++) { try { Thread.sleep(100); } catch (InterruptedException e) { throw new RuntimeException(e); } } System.out.println("/");my }); //观察状态 Thread.State state = thread.getState(); System.out.println(state);//new //观察启动 thread.start(); state=thread.getState(); System.out.println(state);///Run while (state!=Thread.State.TERMINATED){//只要线程不终止,就一直输出状态 Thread.sleep(100); state=thread.getState(); System.out.println(state); } } }
线程优先级
package Demo02Thread; public class TestoPriority { public static void main(String[] args) { System.out.println(Thread.currentThread().getName()+"-->"+Thread.currentThread().getPriority()); Mypriority mypriority=new Mypriority(); Thread my1=new Thread(mypriority); Thread my2=new Thread(mypriority); Thread my3=new Thread(mypriority); Thread my4=new Thread(mypriority); Thread my5=new Thread(mypriority); Thread my6=new Thread(mypriority); my1.start(); my2.setPriority(1); my2.start(); my3.setPriority(4); my3.start(); my4.setPriority(10); my4.start(); } } class Mypriority implements Runnable{ @Override public void run() { System.out.println(Thread.currentThread().getName()+"-->"+Thread.currentThread().getPriority()); } }
守护线程(人类停止,上帝停止)
public class TestDaemon { public static void main(String[] args) { God god=new God(); You you=new You(); Thread thread=new Thread(god); thread.setDaemon(true); thread.start(); new Thread(you).start(); } } class God implements Runnable{ @Override public void run() { while (true){ System.out.println("上帝。。。"); } } } class You implements Runnable{ @Override public void run() { for (int i = 0; i < 365; i++) { System.out.println("人类run...."); } System.out.println("======goodbye"); } }