创建多线程方式一
1.创建:继承Thread+重写run
2.启动:创建子类对象+start
package com.rou.thread;
/**
* Title:创建多线程方式一
* 1.创建:继承Thread+重写run
* 2.启动:创建子类对象+start
* @author RouBao
*
*/
public class StartThread01 extends Thread {
@Override
public void run() {
super.run();
for(int i=0;i<10;i++) {
System.out.println("吃西瓜");
}
}
public static void main(String[] args) {
//创建子类对象
StartThread01 st =new StartThread01();
//启动,不保证立即运行,由cpu调用
st.start();
for(int i=0;i<10;i++) {
System.out.println("抱着火炉");
}
}
}
创建多线程方式二
1.创建:实现Runnable+重写run
2.启动:创建实现类子类+Thread对象+start
3.推荐这种方式:避免单继承的局限性,优先使用接口
4.方便共享资源
package com.rou.thread;
/**
* Title:创建线程方式二:
* 1.创建:实现Runnable+重写run
* 2.启动:创建实现类子类+Thread对象+start
* 3.推荐这种方式:避免单继承的局限性,优先使用接口
* 4.方便共享资源
* @author RouBao
*
*/
public class StartThread02 implements Runnable {
@Override
public void run() {
for(int i=0;i<10;i++) {
System.out.println("大吉大利,今晚吃鸡");
}
}
public static void main(String[] args) {
// 创建实现类对象
StartThread02 st = new StartThread02();
// 创建代理类对象
Thread t = new Thread(st);
// 启动
t.start();
//如果一个对象只是用一次,可以使用匿名,上述代码可写成:
//new Thread(new StartThread()).start();
for(int i=0;i<10;i++) {
System.out.println("欢迎来到王者荣耀");
}
}
}
创建多线程方式三
1.JUC包下实现Callable接口+call方法
2.call方法可抛出异常
3.call方法可设置返回值
例:下载图片
package com.rou.thread;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
/**
* Title:创建线程方式三:
* JUC包下实现Callable接口
* 例:下载图片
* @author RouBao
*
*/
public class CDownloader implements Callable<Boolean> {
private String url;
private String destPath;
public CDownloader(String url,String destPath) {
this.destPath =destPath;
this.url =url;
}
@Override
public Boolean call() throws Exception {
WedDownloader wdl =new WedDownloader();
wdl.Download(url, destPath);
return true;
}
public static void main(String[] args) throws InterruptedException, ExecutionException {
//丢入一个URL地址
CDownloader cd1 =new CDownloader("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1575287798820&di=b4d5e555186be621622f19d7cf53385d&imgtype=0&src=http%3A%2F%2Fi1.article.fd.zol-img.com.cn%2Ft_s640x2000%2Fg5%2FM00%2F0D%2F04%2FChMkJlrYCouIHz1nAAeW99hP9P4AAnvnwCxMmcAB5cP591.jpg","marvel4.jpg");
CDownloader cd2 =new CDownloader("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1575287833861&di=ebe5f21b96fbd48f299b64ce2b2cc8a8&imgtype=0&src=http%3A%2F%2Fn.sinaimg.cn%2Fsinacn%2Fw1600h900%2F20180110%2Ff6ab-fyqnick3045979.jpg","marvel5.jpg");
CDownloader cd3 =new CDownloader("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1575287892704&di=7ad9fe4d698fb52b053d1aec2af42baa&imgtype=jpg&src=http%3A%2F%2F5b0988e595225.cdn.sohucs.com%2Fimages%2F20181001%2F8791de8272c044f7b318224bbf7b55a9.jpeg","marvel6.jpg");
//创建执行服务
ExecutorService ser =Executors.newFixedThreadPool(3);
//提交执行
Future<Boolean> result1 =ser.submit(cd1);
Future<Boolean> result2 =ser.submit(cd2);
Future<Boolean> result3 =ser.submit(cd3);
//获取结果
boolean r1 =result1.get();
boolean r2 =result2.get();
boolean r3 =result3.get();
//关闭服务
ser.shutdownNow();
}
}