2021/8/31 Java多线程1
线程的创建
方法一:继承Thread类,重写Run()方法,创建Thread对象,调用start()方法启动线程
public class Mythread1 extends Thread {
@Override
public void run() {
for (int i = 0; i < 200; i++) {
System.out.println("我是副线程"+i);
}
}
}
- 例程:用
FileUtiles
的copyURLtoFile
方法下载网络图片
public class Mythread2 extends Thread { //线程类
private String url;
private String fileName;
public Mythread2(String url,String fileName){
this.fileName=fileName;
this.url=url;
}
@Override
public void run() {
new WebDownloader().downloader(url,fileName);
}
}
class WebDownloader{ //下载类
public static void downloader(String url,String file){
try {
FileUtils.copyURLToFile(new URL(url),new File(file));
System.out.println(file+"下载成功");
} catch (IOException e) {
e.printStackTrace();
System.out.println("出现下载异常"+file);
}
}
}
方法二:Runnable()接口,实现Runnable接口,创建Runnable对象,new Thread(Runable对象,“name”).start()
跟继承Thread类没什么区别,主要是因为继承Thread类是智能单继承,继承接口就能实现多继承
//用Runnable接口实现抢票
public class MyThread implements Runnable { //线程类
private int ticketNum=10;
@Override
public void run() {
while (true) {
if (ticketNum <= 0) {
break;
} else {
if (Thread.currentThread().getName()=="person1"){
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println(Thread.currentThread().getName() + "买了第" + ticketNum-- + "张票");
}
}
}
public class Aplication { //主类【测试类】
public static void main(String[] args) {
MyThread p1=new MyThread();
new Thread(p1,"person1").start();
new Thread(p1,"person2").start();
new Thread(p1,"person3").start();
}
}
用Runnable接口实现龟兔赛跑
//MyThread类
public class MyThread implements Runnable {
private boolean flag=false;
@Override
public void run() {
for (int i = 0; i <=100 ; i++) {
if (flag){
break;
}
if(Thread.currentThread().getName()=="兔子"){
try {
Thread.sleep(5);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
if (i == 100) {
System.out.println(Thread.currentThread().getName() + "赢了");
flag = true;
} else {
System.out.println(Thread.currentThread().getName() + "跑了"+i+"步");
}
}
}
}
//主类【测试类】
public class Aplication {
public static void main(String[] args) {
MyThread game=new MyThread();
new Thread(game,"乌龟").start();
new Thread(game,"兔子").start();
}
}
方法三:Callable(了解即可)
优点:有返回值、可以抛出异常
静态代理模式【新知识】
静态代理模式有两个特征:
- 需要被代理的真实对象和代理对象都要实现同一个接口
- 真实对象只需要完成自己想做的事情,代理对象需要传入真实对象帮他做另外一些事情
//一个很形象的例子
/*
静态代理案例
被代理的真实对象:结婚的人
代理对象:婚庆公司
*/
public class StaticAgentMode {
public static void main(String[] args) {
new Agent(new RealMarryer("张三")).weMarry();
}
}
interface Marry{
void weMarry();
}
class RealMarryer implements Marry{
private String name;
public RealMarryer(String name) {
this.name = name;
}
@Override
public void weMarry() {
System.out.println(this.name+"结婚了");
}
}
class Agent implements Marry{
private RealMarryer realMarryer;
public Agent(RealMarryer realMarryer) {
this.realMarryer = realMarryer;
}
@Override
public void weMarry() {
beforeMarry();
this.realMarryer.weMarry();
after();
}
void beforeMarry() {
System.out.println("婚礼前准备工作");
}
void after() {
System.out.println("婚礼后善后工作");
}
}
lambda表达式的演化过程
lambda表达式可以使用的前提函数式接口:一个接口如果只有唯一一个抽象方法,就是函数式接口
/*
总结了lambda表达式一步一步简化的过程
从外部类-》静态内部类-》局部内部类-》匿名内部类
*/
public class Lambda {
//2.使用静态内部类
static class LoveImp2 implements Love{
@Override
public void ilove(String name) {
System.out.println("i love"+name);
}
}
public static void main(String[] args) {
//3.使用局部内部类
class LoveImp3 implements Love{
@Override
public void ilove(String name) {
System.out.println("i love"+name);
}
}
Love love=new LoveImp();
love.ilove("gh");
love=new LoveImp2();
love.ilove("gh");
love=new LoveImp3();
love.ilove("gh");
//4.使用匿名内部类
love=new Love() {
@Override
public void ilove(String name) {
System.out.println("i love"+name);
}
};
love.ilove("gh");
//5.lambda表达式
love=(String name)->{
System.out.println("i love"+name);
};
love.ilove("gh");
//lambda表达式简化
love=name->System.out.println("i love"+name);
love.ilove("gh");
}
}
interface Love {
void ilove(String name);
}
//1.正常使用外部类
class LoveImp implements Love{
@Override
public void ilove(String name) {
System.out.println("i love"+name);
}
}