实现callable接口(了解即可)
-
实现callable接口,需要返回值类型
-
重写call方法,需要抛出异常
-
创建目标对象
-
创建执行服务:ExecutorService ser=Executor.newFixedThreadPool(1);
-
提交服务
package com.fei.demo01;
import org.apache.commons.io.FileUtils;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.concurrent.*;
public class TextCallbale implements Callable<Boolean> {
private String url;
private String name;
public TextCallbale(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 {
TextCallbale t1=new TextCallbale("https://gimg2.baidu.com/image_search/src=http%3A%2F%2Flmg.jj20.com%2Fup%2Fallimg%2F1114%2F010421143P0%2F210104143P0-4-1200.jpg&refer=http%3A%2F%2Flmg.jj20.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1668094322&t=1222fed63d4d2315eb80ed377579ec90","1.jpg");
TextCallbale t2=new TextCallbale("https://gimg2.baidu.com/image_search/src=http%3A%2F%2Flmg.jj20.com%2Fup%2Fallimg%2F1114%2F010421143P0%2F210104143P0-4-1200.jpg&refer=http%3A%2F%2Flmg.jj20.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1668094322&t=1222fed63d4d2315eb80ed377579ec90","2.jpg");
TextCallbale t3=new TextCallbale("https://gimg2.baidu.com/image_search/src=http%3A%2F%2Flmg.jj20.com%2Fup%2Fallimg%2F1114%2F010421143P0%2F210104143P0-4-1200.jpg&refer=http%3A%2F%2Flmg.jj20.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1668094322&t=1222fed63d4d2315eb80ed377579ec90","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) {
throw new RuntimeException(e);
}
}
}
Lambda
-
理解Functional Interface(函数式接口)是学习Java8 lambda表达式的关键所在。
-
函数式接口的定义:
-
任何接口,如果只包含唯一一个抽象方法,那么它就是一个函数式接口。
public interface Runnable{
public abstract void run();
} -
对于函数式接口,我们可以通过lambda表达式来创建该接口的对象。
-
package com.fei.lambda;
public class TestLambda1 {
//静态内部类
static class Like2 implements Ilike{
@Override
public void lamdba() {
System.out.println("I like lambda2");
}
}
public static void main(String[] args) {
Ilike like = new Like();
like.lamdba();
Like2 like2 = new Like2();
like2.lamdba();
//局部内部类
class Like3 implements Ilike{
@Override
public void lamdba() {
System.out.println("I like lambda3");
}
}
Like3 like3 = new Like3();
like3.lamdba();
//匿名内部类,没有类的名称,必须借助接口或者父类
like = new Ilike() {
@Override
public void lamdba() {
System.out.println("I like lambda4");
}
};
like.lamdba();
//用lambda简化
like=()-> {
System.out.println("I like lambda4");
};
like.lamdba();
}
}
//1。定义一个函数式接口
interface Ilike{
void lamdba();
}
//2.实现类
class Like implements Ilike{
@Override
public void lamdba() {
System.out.println("I like lambda");
}
}
package com.fei.lambda;
public class TestLambda1 {
//静态内部类
static class Like2 implements Ilike{
@Override
public void lamdba() {
System.out.println("I like lambda2");
}
}
public static void main(String[] args) {
Ilike like = new Like();
like.lamdba();
Like2 like2 = new Like2();
like2.lamdba();
//局部内部类
class Like3 implements Ilike{
@Override
public void lamdba() {
System.out.println("I like lambda3");
}
}
Like3 like3 = new Like3();
like3.lamdba();
//匿名内部类,没有类的名称,必须借助接口或者父类
like = new Ilike() {
@Override
public void lamdba() {
System.out.println("I like lambda4");
}
};
like.lamdba();
//用lambda简化
like=()-> {
System.out.println("I like lambda4");
};
like.lamdba();
}
}
//1。定义一个函数式接口
interface Ilike{
void lamdba();
}
//2.实现类
class Like implements Ilike{
@Override
public void lamdba() {
System.out.println("I like lambda");
}
}
线程状态
线程休眠
package com.fei.demo01;
import java.text.SimpleDateFormat;
import java.util.Date;
public class TestSleep2 {
public static void main(String[] args) {
Date date = new Date(System.currentTimeMillis());//获取当前系统时间
while (true){
try {
Thread.sleep(1000);
System.out.println(new SimpleDateFormat("HH:mm:ss").format(date));
date = new Date(System.currentTimeMillis());//更新系统时间
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
//模拟倒计时
public void tenDown() throws InterruptedException {
int num = 10;
while (true){
Thread.sleep(1000);
System.out.println(num--);
if (num<=0){
break;
}
}
}
}
线程礼让
-
礼让线程,让当前正在执行的线程暂停,但不阻塞。
-
将线程从运行状态转为就绪状态。
-
让cpu重新调度,礼让不一定成功,看cpu心情。
package com.fei.demo01;
//礼让不一定成功,看cpu心情
public class TestYield {
public static void main(String[] args) {
MyYield myYield = new MyYield();
new Thread(myYield,"a").start();
new Thread(myYield,"b").start();
}
}
class MyYield implements Runnable{
@Override
public void run() {
System.out.println(Thread.currentThread().getName()+"线程开始执行");
Thread.yield();
System.out.println(Thread.currentThread().getName()+"线程结束执行");
}
}
Join
-
Join合并线程,待此线程执行完成后,在执行其他线程,其他线程堵塞。
-
可以想象成插队。
package com.fei.demo01;标签:13,like,void,lamdba,System,callable,new,2022.10,public From: https://www.cnblogs.com/Paytonfei/p/16786671.html
//测试Join方法,可以理解为插队
public class TestJoin implements Runnable{
@Override
public void run() {
for (int i = 0; i < 1000; i++) {
System.out.println("线程vip来了"+i);
}
}
public static void main(String[] args) throws InterruptedException {
//启动线程
TestJoin testJoin = new TestJoin();
Thread thread = new Thread(testJoin);
thread.start();
//主线程
for (int i = 0; i < 500; i++) {
if (i==200){
thread.join();//插队
}
System.out.println("main"+i);
}
}
}