java线程实现的三种方式以及静态代理
点击上方蓝色字体,选择“标星公众号”
优质文章,第一时间送达
作者 | Sprinining
来源 | urlify.cn/aIZZFb
线程
一个进程中若开辟多个线程,线程的运行由调度器控制,先后顺序不能人为干预。
实现方式
继承 Thread类
调用run方法,只有主线程一条路
调用start方法,主线程和子线程并行交替执行
导入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
//开启线程,不一定立刻执行,由cpu调度
myThread.start();//同时运行,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 {
}
}
}
实现Runnable接口
推荐使用,避免单继承局限性,灵活方便,方便同一个对象被多个线程使用
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) {
//创建实现runnable接口的类对象
MyThread3 myThread3 = new MyThread3();
//创建线程对象,通过线程对象启动线程,代理
new Thread(myThread3).start();
for (int i = 0; i < 20; i++) {
System.out.println("haha");
}
}
}
多线程同时操作一个对象
public class MyThread4 implements Runnable{
private int ticketNums = 10;
@Override
public void run() {
while (true){
if(ticketNums <= 0)
break;
//模拟延时
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();
}
}
//多个线程操作同一个资源时,线程不安全,数据紊乱
/*
haha拿到了第9张票
xixi拿到了第8张票
hehe拿到了第10张票
hehe拿到了第6张票
xixi拿到了第7张票
haha拿到了第5张票
hehe拿到了第4张票
haha拿到了第4张票
xixi拿到了第4张票
xixi拿到了第2张票
hehe拿到了第3张票
haha拿到了第1张票
*/
实现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");
//创建执行服务
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();
//关闭服务
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 {
}
}
}
静态代理
真实对象和代理对象都要实现同一个接口
代理对象要代理真实角色
代理对象可以做很多真实对象做不了的事情
真实对象专注做自己的事情
public class StaticProxy {
public static void main(String[] args) {
You you = new You();
//代理真实对象runnable
new Thread(()-> System.out.println("haha")).start();
//代理真实对象you
new WeddingCompany(new You()).HappyMarry();
WeddingCompany weddingCompany = new WeddingCompany(you);
weddingCompany.HappyMarry();
}
}
interface Marry{
void HappyMarry();
}
//真实角色
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();//真实对象
after();
}
public void before() {
System.out.println("before marry");
}
public void after() {
System.out.println("after marry");
}
}
锋哥最新SpringCloud分布式电商秒杀课程发布
👇👇👇
👆长按上方微信二维码 2 秒
感谢点赞支持下哈
评论