java線程實(shí)現(xiàn)的三種方式以及靜態(tài)代理
點(diǎn)擊上方藍(lán)色字體,選擇“標(biāo)星公眾號(hào)”
優(yōu)質(zhì)文章,第一時(shí)間送達(dá)
作者 | Sprinining
來源 | urlify.cn/aIZZFb
76套java從入門到精通實(shí)戰(zhàn)課程分享
線程
一個(gè)進(jìn)程中若開辟多個(gè)線程,線程的運(yùn)行由調(diào)度器控制,先后順序不能人為干預(yù)。
實(shí)現(xiàn)方式
繼承 Thread類
調(diào)用run方法,只有主線程一條路
調(diào)用start方法,主線程和子線程并行交替執(zhí)行
導(dǎo)入common-io.jar下載圖片
public class MyThread extends Thread{
@Override
public void run() {
for (int i = 0; i < 20; i++) {
System.out.println("xixi");
}
}
public static void main(String[] args) {
MyThread myThread = new MyThread();
//myThread.run();只有主線程一條路,先xixi后haha
//開啟線程,不一定立刻執(zhí)行,由cpu調(diào)度
myThread.start();//同時(shí)運(yùn)行,xixi、haha交替
//main線程
for (int i = 0; i < 20; i++) {
System.out.println("haha");
}
}
}
import org.apache.commons.io.FileUtils;
import java.io.File;
import java.io.IOException;
import java.net.URL;
public class MyThread2 extends Thread{
private String url;
private String name;
public MyThread2(String url, String name){
this.name = name;
this.url = url;
}
@Override
public void run() {
WebDownloader webDownloader = new WebDownloader();
webDownloader.downloader(url, name);
System.out.println("下載了文件名為" + name);
}
public static void main(String[] args) {
MyThread2 t1 = new MyThread2("https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=3246675119,2528014287&fm=26&gp=0.jpg", "csgo1.jpg");
MyThread2 t2 = new MyThread2("https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=3246675119,2528014287&fm=26&gp=0.jpg", "csgo2.jpg");
MyThread2 t3 = new MyThread2("https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=3246675119,2528014287&fm=26&gp=0.jpg", "csgo3.jpg");
t1.start();
t2.start();
t3.start();
}
}
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("downloader異常");
} finally {
}
}
}
實(shí)現(xiàn)Runnable接口
推薦使用,避免單繼承局限性,靈活方便,方便同一個(gè)對(duì)象被多個(gè)線程使用
public class MyThread3 implements Runnable{
@Override
public void run() {
for (int i = 0; i < 20; i++) {
System.out.println("xixi");
}
}
public static void main(String[] args) {
//創(chuàng)建實(shí)現(xiàn)runnable接口的類對(duì)象
MyThread3 myThread3 = new MyThread3();
//創(chuàng)建線程對(duì)象,通過線程對(duì)象啟動(dòng)線程,代理
new Thread(myThread3).start();
for (int i = 0; i < 20; i++) {
System.out.println("haha");
}
}
}
多線程同時(shí)操作一個(gè)對(duì)象
public class MyThread4 implements Runnable{
private int ticketNums = 10;
@Override
public void run() {
while (true){
if(ticketNums <= 0)
break;
//模擬延時(shí)
try {
Thread.sleep(20);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + "拿到了第" + ticketNums-- + "張票");
}
}
public static void main(String[] args) {
MyThread4 myThread4 = new MyThread4();
new Thread(myThread4, "haha").start();
new Thread(myThread4, "xixi").start();
new Thread(myThread4, "hehe").start();
}
}
//多個(gè)線程操作同一個(gè)資源時(shí),線程不安全,數(shù)據(jù)紊亂
/*
haha拿到了第9張票
xixi拿到了第8張票
hehe拿到了第10張票
hehe拿到了第6張票
xixi拿到了第7張票
haha拿到了第5張票
hehe拿到了第4張票
haha拿到了第4張票
xixi拿到了第4張票
xixi拿到了第2張票
hehe拿到了第3張票
haha拿到了第1張票
*/實(shí)現(xiàn)Callable接口
可以定義返回值、可以拋出異常
import org.apache.commons.io.FileUtils;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.concurrent.*;
public class MyCallable implements Callable<Boolean> {
private String url;
private String name;
public MyCallable(String url, String name){
this.name = name;
this.url = url;
}
@Override
public Boolean call() throws Exception {
WebDownloader webDownloader = new WebDownloader();
webDownloader.downloader(url, name);
System.out.println("下載了文件名為" + name);
return true;
}
public static void main(String[] args) throws ExecutionException, InterruptedException {
MyCallable t1 = new MyCallable("https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=3246675119,2528014287&fm=26&gp=0.jpg", "csgo1.jpg");
MyCallable t2 = new MyCallable("https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=3246675119,2528014287&fm=26&gp=0.jpg", "csgo2.jpg");
MyCallable t3 = new MyCallable("https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=3246675119,2528014287&fm=26&gp=0.jpg", "csgo3.jpg");
//創(chuàng)建執(zhí)行服務(wù)
ExecutorService ser = Executors.newFixedThreadPool(3);
//提交執(zhí)行
Future<Boolean> r1 = ser.submit(t1);
Future<Boolean> r2 = ser.submit(t2);
Future<Boolean> r3 = ser.submit(t3);
//獲取結(jié)果
boolean rs1 = r1.get();
boolean rs2 = r2.get();
boolean rs3 = r3.get();
//關(guān)閉服務(wù)
ser.shutdown();
}
}
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("downloader異常");
} finally {
}
}
}
靜態(tài)代理
真實(shí)對(duì)象和代理對(duì)象都要實(shí)現(xiàn)同一個(gè)接口
代理對(duì)象要代理真實(shí)角色
代理對(duì)象可以做很多真實(shí)對(duì)象做不了的事情
真實(shí)對(duì)象專注做自己的事情
public class StaticProxy {
public static void main(String[] args) {
You you = new You();
//代理真實(shí)對(duì)象runnable
new Thread(()-> System.out.println("haha")).start();
//代理真實(shí)對(duì)象you
new WeddingCompany(new You()).HappyMarry();
WeddingCompany weddingCompany = new WeddingCompany(you);
weddingCompany.HappyMarry();
}
}
interface Marry{
void HappyMarry();
}
//真實(shí)角色
class You implements Marry{
@Override
public void HappyMarry() {
System.out.println("marry!");
}
}
//代理角色
class WeddingCompany implements Marry{
private Marry target;
public WeddingCompany(Marry target){
this.target = target;
}
@Override
public void HappyMarry() {
before();
this.target.HappyMarry();//真實(shí)對(duì)象
after();
}
public void before() {
System.out.println("before marry");
}
public void after() {
System.out.println("after marry");
}
}
鋒哥最新SpringCloud分布式電商秒殺課程發(fā)布
??????
??長(zhǎng)按上方微信二維碼 2 秒
感謝點(diǎn)贊支持下哈 
評(píng)論
圖片
表情
