前言
- 本文是学自狂神说java整理的笔记,并非里面的内容都有认真的整理、代码也有认真的练习
- 本文仅供学习和交流之用,禁止非商业用途
多线程是模拟出来的,真正的多线程是指有多个cpu,即多核,如服务器。如果是模拟出来的多线程,即在一个cpu的情况下,在同一个时间点,cpu只能执行一个代码,因为切换的很快,所以就有同时执行的错觉。
package 多线程.Day01;
/**
* @author 缘友一世
* date 2022/11/1-22:09
*/
//创建线程方式一:继承Thread类,重写run()方法,调用start开启线程
public class TestThread1 extends Thread{
@Override
public void run() {
//run方法线程体
for (int i=0;i<10;i++) {
System.out.println("我在看代码"+i);
}
}
public static void main(String[] args) {
//main线程,主线程
//创建一个线程对象
TestThread1 testThread1 = new TestThread1();
//调用start方法开启线程
testThread1.run();
for(int i=0;i<10;i++) {
System.out.println("我在主线程"+i);
}
}
}
package 多线程.Day01;
/**
* @author 缘友一世
* date 2022/11/1-22:09
*/
//创建线程方式一:继承Thread类,重写run()方法,调用start开启线程
public class TestThread1 extends Thread{
@Override
public void run() {
//run方法线程体
for (int i=0;i<10;i++) {
System.out.println("我在看代码"+i);
}
}
public static void main(String[] args) {
//main线程,主线程
//创建一个线程对象
TestThread1 testThread1 = new TestThread1();
//调用start方法开启线程
testThread1.start();
for(int i=0;i<10;i++) {
System.out.println("我在主线程"+i);
}
}
}
package 多线程.Day01;
import org.apache.commons.io.FileUtils;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
/**
* @author 缘友一世
* date 2022/11/1-22:28
*/
//练习Thread,实现多线程同步下载图片
public class TestThread2 extends Thread{
private String url;//网络图片地址
private String name;//保存的文件名
public TestThread2(String url, String name) {
this.url = url;
this.name = name;
}
//下载文件的执行体
@Override
public void run() {
WebDownloader webDownloader = new WebDownloader();
try {
webDownloader.downloader(url,name);
System.out.println("成功下载文件"+name);
} catch (MalformedURLException e) {
throw new RuntimeException(e);
}
}
public static void main(String[] args) {
TestThread2 t1 = new TestThread2("https://img-home.csdnimg.cn/images/20221101051344.jpg","10240.jpg");
TestThread2 t2 = new TestThread2("https://img-home.csdnimg.cn/images/20221101051344.jpg","10241.jpg");
TestThread2 t3 = new TestThread2("https://img-home.csdnimg.cn/images/20221101051344.jpg","10242.jpg");
t1.start();
t2.start();
t3.start();
}
}
class WebDownloader{
public void downloader(String url,String name) throws MalformedURLException {
try {
FileUtils.copyURLToFile(new URL(url),new File(name));
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
package 多线程.Day01;
/**
* @author 缘友一世
* date 2022/11/1-22:53
*/
//创建线程的方式二:实现runnable接口,重写run方法,执行线程需要放入到runnable接口实现类,调用start方法
public class TestThread3 implements Runnable{
@Override
public void run() {
//线程体
for(int i=0;i<10;i++) {
System.out.print("我数数"+i+" ");
}
}
public static void main(String[] args) {
//创建runnable接口实现类对象
TestThread3 testThread3 = new TestThread3();
//创建线程对象,通过线程对象来开启我们的线程,代理
// Thread thread = new Thread(testThread3);
// //启动
// thread.start();
new Thread(testThread3).start();
for(int i=0;i<10;i++) {
System.out.print("我看书"+i+" ");
}
}
}
package 多线程.Day01;
import org.apache.commons.io.FileUtils;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
/**
* @author 缘友一世
* date 2022/11/1-22:28
*/
//练习Thread,实现多线程同步下载图片
public class TestThread4 implements Runnable{
private String url;//网络图片地址
private String name;//保存的文件名
public TestThread4(String url, String name) {
this.url = url;
this.name = name;
}
//下载文件的执行体
@Override
public void run() {
WebDownloader2 webDownloader = new WebDownloader2();
try {
webDownloader.downloader(url,name);
System.out.println("成功下载文件"+name);
} catch (MalformedURLException e) {
throw new RuntimeException(e);
}
}
public static void main(String[] args) {
TestThread2 t1 = new TestThread2("https://img-home.csdnimg.cn/images/20221101051344.jpg","10240.jpg");
TestThread2 t2 = new TestThread2("https://img-home.csdnimg.cn/images/20221101051344.jpg","10241.jpg");
TestThread2 t3 = new TestThread2("https://img-home.csdnimg.cn/images/20221101051344.jpg","10242.jpg");
new Thread(t1).start();
new Thread(t2).start();
new Thread(t3).start();
}
}
class WebDownloader2{
public void downloader(String url,String name) throws MalformedURLException {
try {
FileUtils.copyURLToFile(new URL(url),new File(name));
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
//一份资源
StartThread station=newStartThread();
//多个代理
newThread(station,name:小明”).start();
newThread(station,name:“老师”).start();
newThread(station,name:小红”).start();
package 多线程.Day01;
/**
* @author 缘友一世
* date 2022/11/2-9:11
*/
//多个线程同时操作同一个对象
//买票问题
//问题:多个线程操作同一个资源的情况下,线程不安全,数据絮乱
public class TestThread5 implements Runnable{
/**票数*/
private int ticketNums=10;
@Override
public void run() {
while(ticketNums>=0) {
if(ticketNums==0) {
break;
}
try {
Thread.sleep(200);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
System.out.println(Thread.currentThread().getName()+"-->拿到了第"+ticketNums--+"票");
}
}
public static void main(String[] args) {
TestThread5 ticket = new TestThread5();
new Thread(ticket,"小明").start();
new Thread(ticket,"小亮").start();
new Thread(ticket,"小光").start();
new Thread(ticket,"小红").start();
}
}
package 多线程.Day01;
/**
* @author 缘友一世
* date 2022/11/2-9:40
*/
public class TestThread6 implements Runnable{
/**胜利者*/
private static String winner;
@Override
public void run() {
for(int i=1;i<=100;i++) {
//兔子每20步要休息一下
if(Thread.currentThread().getName().equals("兔子") && i%20==0) {
try {
Thread.sleep(1);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
System.out.println(Thread.currentThread().getName()+"-->跑了"+i+"步");
//判断比赛是否结束,结束就终止程序
if(gameOver(i)) {
break;
}
}
}
//判断比赛是否结束
private boolean gameOver(int steps) {
//判断是否有胜利者
if(winner!=null) {//已经存在胜利者了
return true;
}{
if(steps>=100) {
winner=Thread.currentThread().getName();
System.out.println("胜利者是"+winner);
return true;
}
}
return false;
}
public static void main(String[] args) {
TestThread6 race = new TestThread6();
new Thread(race,"乌龟").start();
new Thread(race,"兔子").start();
}
}
package 多线程.Day01;
import org.apache.commons.io.FileUtils;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.concurrent.*;
/**
* @author 缘友一世
* date 2022/11/2-17:11
*/
//创建线程方式三:实现callable接口
public class TestCallable implements Callable<Boolean> {
private String url;//网络图片地址
private String name;//保存的文件名
public TestCallable(String url, String name) {
this.url = url;
this.name = name;
}
//下载文件的执行体
@Override
public Boolean call() {
WebDownloader webDownloader = new WebDownloader();
try {
webDownloader.downloader(url,name);
System.out.println("成功下载文件"+name);
return true;
} catch (MalformedURLException e) {
throw new RuntimeException(e);
}
}
public static void main(String[] args) throws ExecutionException, InterruptedException {
TestCallable t1 = new TestCallable("https://img0.baidu.com/it/u=1348230343,3476037199&fm=253&fmt=auto&app=138&f=JPEG?w=889&h=500","sky1.jpg");
TestCallable t2 = new TestCallable("https://img0.baidu.com/it/u=1348230343,3476037199&fm=253&fmt=auto&app=138&f=JPEG?w=889&h=500","sky2.jpg");
TestCallable t3 = new TestCallable("https://img0.baidu.com/it/u=1348230343,3476037199&fm=253&fmt=auto&app=138&f=JPEG?w=889&h=500","sky3.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 res1 = r1.get();
Boolean res2 = r2.get();
Boolean res3 = r3.get();
//关闭服务
ser.shutdownNow();
}
}
class WebDownloader3{
public void downloader(String url,String name) throws MalformedURLException {
try {
FileUtils.copyURLToFile(new URL(url),new File(name));
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
package 多线程.Day01;
/**
* @author 缘友一世
* date 2022/11/2-18:13
*/
//静态代理模式总结
//真实对象和代理对象都要实现同一个接口
//该对象要代理真实角色
//好处
//代理象可以做很多真实对象做不了的事情
//真实对象专注做自己的事情。
public class StaticProxy {
public static void main(String[] args) {
/*
new Thread(new Runnable() {
@Override
public void run() {
}
}).start();
*/
new Thread(()-> System.out.println("我爱你")).start();
//WeddingCompany weddingCompany = new WeddingCompany(new You());
//weddingCompany.HappyMary();
new WeddingCompany(new You()).HappyMary();
}
}
interface Marry{
void HappyMary();
}
//真实角色
class You implements Marry {
@Override
public void HappyMary() {
System.out.println("小王子结婚了!");
}
}
//代理角色,帮助结婚
class WeddingCompany implements Marry{
//真实目标角色
private Marry target;
public WeddingCompany(Marry target) {
this.target = target;
}
@Override
public void HappyMary() {
before();
this.target.HappyMary();
after();
}
private void before() {
System.out.println("结婚之前的事情处理");
}
private void after() {
System.out.println("结婚之后的事情处理");
}
}
- 再有些时候,我们遇到某些类只是使用一次,刚开始我们定义在外部【外部类】,然后改进到内部类【匿名内部类】,这样就会造成匿名内部类定义过多。
package 多线程.Lambda;
/**
* @author 缘友一世
* date 2022/11/2-19:06
*/
/*
推到lambda表达式
*/
public class TestLambda1 {
//3 静态内部类
static class Like2 implements ILike {
@Override
public void Lambda() {
System.out.println("2 静态内部类的lambda");
}
}
public static void main(String[] args) {
ILike like = new Like();
like.Lambda();
like = new Like2();
like.Lambda();
//4 局部内部类
class Like3 implements ILike {
@Override
public void Lambda() {
System.out.println("3 局部内部类lambda");
}
}
like=new Like3();
like.Lambda();
//5 匿名内部类
like=new ILike() {
@Override
public void Lambda() {
System.out.println("4 匿名内部类的lambda");
}
};
like.Lambda();
//6 用lambda简化 舍弃接口和方法
ILike like2=()->{
System.out.println("5 用lambda简化的lambda");
};
like2.Lambda();
}
}
//1 定义一个函数式接口
interface ILike{
void Lambda();
}
//2 实现类
class Like implements ILike {
@Override
public void Lambda() {
System.out.println("1 外部实现类的lambda");
}
}
package 多线程.Lambda;
/**
* @author 缘友一世
* date 2022/11/2-19:52
*/
//1 定义函数式接口
interface ILove{
void love(String name1,String name2,String name3);
}
public class TestLambda2 {
public static void main(String[] args) {
ILove iLove=null;
//1 匿名内部类
iLove = new ILove() {
@Override
public void love(String name1, String name2, String name3) {
System.out.println("匿名内部类-->" + name1 + "和" + name2 + "爱上了" + name3);
}
};
iLove.love("小黄","小黑","小花");
//前提是接口为函数式接口。
//1 lambda简化
//Book参数也可以去掉参数类型。 要去掉,必须都去掉
iLove=(String name1,String name2,String name3) -> {
System.out.println("lambda简化1-->"+name1+"和"+name2+"爱上了"+name3);
};
iLove.love("小黄","小黑","小花");
//2 简化括号
iLove=(name1,name2,name3)->{
System.out.println("lambda简化2-->"+name1+"和"+name2+"爱上了"+name3);
};
iLove.love("小黄","小黑","小花");
//3 去掉花括号【代码只有一行的情况下】
iLove=(name1,name2,name3)->System.out.println("lambda简化3-->"+name1+"和"+name2+"爱上了"+name3);
iLove.love("小黄","小黑","小花");
//如果有多行,那么就用代码块包裹。
iLove=(name1,name2,name3)->{
System.out.println("lambda简化4-->"+name1+"和"+name2+"爱上了"+name3);
System.out.println("lambda简化4-->"+name1+"和"+name2+"抛弃了"+name3);
};
iLove.love("小黄","小黑","小花");
}
}