java 多线程

admin 2020年05月13日 •  教程 Java 299
本文最后修改于 398 天前,部分内容可能已经过时!

多线程

线程的3种创建

  • 集成Thread 类
  • 实现Runnable接口
  • 实现callable接口

创建线程方式一

继承thread类 重写run方法 调用start方法开启线程

public class TestThread extends Thread {

    @Override
    public void run() {
        //run方法 线程题
        for (int i = 0; i < 20; i++) {
            System.out.println("i run线程= " + i);
        }
    }

    public static void main(String[] args) {
        //创建多线程的对象
        TestThread testThread = new TestThread();
        //开启线程
        testThread.start();
        //main 线程体
        for (int i = 0; i < 200; i++) {
            System.out.println("i main线程= " + i);
        }
    }
}

运行结果:

mark

总结 方法调用时一定调用 [start]()方法

注意线程开启不一定立即执行 由cpu调度执行

创建线程方式二

public class TestRunnable  implements Runnable{


    public void run() {
        //run方法 线程题
        for (int i = 0; i < 20; i++) {
            System.out.println("i run线程= " + i);
        }
    }

    public static void main(String[] args) {


        TestRunnable testRunnable = new TestRunnable();
        Thread thread = new Thread(testRunnable);
        thread.start();
        //main 线程体
        for (int i = 0; i < 200; i++) {
            System.out.println("i main线程= " + i);
        }
    }
}

与继承Therad类的区别

两种方法都是重写实现run方法 线程体都是一样的

启动方式:

runnable 接口 是先new Thread 把实现runnable接口的类传入Thread 类 进行start 调用 ,,, 如 new Thread(类).staer();

运行结果

mark

mark

创建线程方式三

与前面两种方式不同

mark

实现callable 接口 企业级(瞎说的)应用的多

他的可多用性 返回值 可任意类型 线程池

代码实现

class TesTCallable implements Callable<Integer>{

    public Integer call() throws Exception {
        for (int i = 0; i < 200; i++) {
            System.out.println("i call线程体= " + i);
        }
        return 1;
    }

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        TesTCallable tesTCallable = new TesTCallable();


        //首先需要创建线程
        ExecutorService ser = Executors.newFixedThreadPool(3);

        //运行线程体
        Future<Integer> submit = ser.submit(tesTCallable);
        for (int i = 0; i < 2000; i++) {
            System.out.println("i 我是主线程= " + i);
        }
        //获取返回值  这里可以选择获取异常或者抛出
        Integer integer = submit.get();
        System.out.println("integer = " + integer);


        ser.shutdownNow();




    }
}

运行结果 交叉输出

mark

Thread 原理

thread 原理基于静态代理模式 java设计模式的一种

mark

Thread类 同样实现了Runnable接口 重写了 run()方法

mark

启动线程 start()方法

比如: 房东 要出租房子

​ 你要 租房子

这个时候静态代理就来了 (中介)

这个中介就可以说是静态代理

房东找→中介出租房子 房东只管出租房子 不需要管任何事务 比如签合同带你去看房子,这些 都是中介来完成 ,房东只负责单一业务

你 →找中介租房子 你也只管租房子,不需要管合同 一系列复杂的事情 只管找中介租房子 其余的全部由中介负责

Lamda 表达式 实现多线程

mark

lambda 简化过程

/*推导lambda 表达式*/
public class TestLambda {

    //3.静态内部类
    static class Like2 implements  ILike{

        public void lambda() {
            System.out.println("I Like Lambda2");
        }
    }

    public static void main(String[] args) {
        ILike iLike = new Like();
        iLike.lambda();
        iLike= new Like2();
        iLike.lambda();
        //4.局部内部类   类在方法体内
        class Like3 implements  ILike{
            public void lambda() {
                System.out.println("I Like Lambda3");
            }
        }
        iLike= new Like3();
        iLike.lambda();


        //5.匿名内部类

        iLike = new ILike() {
            public void lambda() {
                System.out.println("I Like Lambda4");
            }
        } ;
        iLike.lambda();
        //6.lambda  简化
        iLike = ()->{
            System.out.println("I Like Lambda5");
        };
        iLike.lambda();

    }

}
//1.定义一个函数式接口
interface ILike{

    void lambda();
}

//2.实现函数式接口 类
class Like implements  ILike{

    public void lambda() {
        System.out.println("I Like Lambda");
    }
}

线程状态

  • 创建状态
  • 就绪状态
  • 阻塞状态
  • 运行状态
  • 死亡状态

mark

更详细的图

mark

线程的方法

mark

Tags:java多线程
上一篇
下一篇

添加新评论

已有 299 条评论

 Innofwag 7 个月前 • |

buy cheap cialis online

buy cialis online without a prescription

cut cialis pills

safe cialis online pharmacy

 PhilipIndes 7 个月前 • |

Powerball drawings take place every Wednesday and Saturday in Tallahassee, Florida, at approximately 10:59pm ET.
545689.0/ CLAAS / CLAAS MEDION 320/310 / LEXION 480 цена-351,52.
В· There were 326,810 tickets sold that won at least $4 in the drawing. In Michigan, 12,898 tickets sold won at least $4.
https://easylotto.co.za/lotto-scandals.html
https://easylotto.co.za/play-to-win-the-game.html
https://easylotto.co.za/que-dias-juega-el-powerball.html
How to play. How Old Can a Lottery Ticket Be to Turn in to Win? в†’

 Amyreata 7 个月前 • |

order real viagra online

 djothdfhyCax 7 个月前 • |

cialis online for sale
generic for viagra
how to buy generic cialis online
hykmnsag.com
mail order viagra australia

 DurekShuch 7 个月前 • |

Thanks a lot. Lots of facts!
how to write a personal narrative essay essay writing services review thesis defended

 PhilipIndes 7 个月前 • |

So go forth and play if you wish! And remember: No matter what happens tonight, you're still a winner to me! But also, if you do win, I have to talk to you about possibly donating some cash for my extremely pressing burrito research.
2+ players “cause of death: killed by a hateful friend.”
Disclaimer.
https://win2lotto.co.za/find-unclaimed-money-through-sweepstakes-and-lottery.html
https://win2lotto.co.za/mega-millions-results-1119.html
https://win2lotto.co.za/who-won-the-lottery-in-florida.html
Montana Cash | $290,000. Alaska.

 White Paper Writers 7 个月前 • |

compare and contrast essay high school and college essay 1984

 MichaelPsync 7 个月前 • |

According to the Powerball website, Johnson is planning to pay off her student loans, take a vacation with her mom, and then save the remainder.
PowerballВ® Mega MillionsВ® Lotto TexasВ® Texas Two StepВ® All or Nothingв„ў Pick 3в„ў Daily 4в„ў Cash FiveВ® QUICKTICKETв„ў Receipt Ticket Scratch Tickets.
A Powerball ticket sold in Texas during September 2019. Scott McDonald/Newsweek.
https://youlotto.co.za/is-lucky-star-good.html
https://youlotto.co.za/out-2-win.html
https://youlotto.co.za/superbowl-prop-bets-results.html
В· The next Lotto 47 drawing is also Wednesday night, Jan. 16 at 7:29 p.m. with a jackpot of $1.55 million. Deadline is 7:08 p.m. that day. (c) Manner of conducting drawings.

 BrenoPruck 7 个月前 • |

Very good content. Thanks a lot.
write an argument essay custom essay writing services essay writing service review