线程-Thread

蔚落 2021-12-22 02:49 489阅读 0赞

线程

线程和进程的区别:

①线程是程序中一个单一的顺序控制流程。进程内一个相对独立的、可调度的执行单元,是系统独立调度和分派CPU的基本单位指运行中的程序的调度单位。

在单个程序中同时运行多个线程完成不同的工作,称为多线程。

②进程(Process)是计算机中的程序关于某数据集合上的一次运行活动,是系统进行资源分配和调度的基本单位,是操作系统结构的基础

进程和线程的关系:

1700523-20190613104751017-1716058620.png

并发和并行:

并行:多个线程可以同时执行,每一个时间段,可以有多个线程同时执行。

1700523-20190613105408131-1721017423.png

并发:多个线程同时竞争一个位置,竞争到的才可以执行,每一个时间段只有一个线程在执行。

1700523-20190613105428996-34462648.png

线程的4种创建方式:

1.继承Thread类创建线程

2.实现Runnable接口创建线程

3.使用Callable和Future创建线程

4.使用线程池例如用Executor框架

一,继承Thread类创建线程

  1. public class ThreadTest extends Thread{
  2. public void run(){
  3. for(int i=1;i<=20;i++){
  4. System.out.println(i+".你好,来自线程"+Thread.currentThread().getName());
  5. }
  6. }
  7. }

测试类及运行结果:

  1. public class TestCase {
  2. public static void main(String[] args) {
  3. ThreadTest one=new ThreadTest();
  4. ThreadTest one1=new ThreadTest();
  5. one.start();
  6. one1.start();
  7. }
  8. }

1700523-20190614175905848-508908321.png

2.实现Runnable接口创建线程

  1. public class RunnableTest implements Runnable{
  2. @Override
  3. public void run() {
  4. for(int i=1;i<=20;i++){
  5. System.out.println(i+".你好,来自线程"+Thread.currentThread().getName());
  6. }
  7. }
  8. }

测试类及运行结果:

  1. public class TestRunnable {
  2. public static void main(String[] args) {
  3. RunnableTest rt=new RunnableTest();
  4. Thread t1=new Thread(rt);
  5. Thread t2=new Thread(rt);
  6. t1.start();
  7. t2.start();
  8. }
  9. }

1700523-20190614180133492-1515442851.png

3.使用Callable和Future创建线程

  1. public class CallAbleTest implements Callable<String>{
  2. @Override
  3. //拥有返回值和可以抛异常的方法体
  4. public String call() throws Exception {
  5. System.out.println("call的名字:"+Thread.currentThread().getName());
  6. return "我是call";
  7. }
  8. }

测试类和运行结果:

  1. public class TestCall614 {
  2. public static void main(String[] args) {
  3. //创建FutureTask对象来包装CallAbleTest
  4. FutureTask<String> ft=new FutureTask<String>(new CallAbleTest());
  5. //创建线程对象
  6. Thread th=new Thread(ft,"CallAble");
  7. th.start();
  8. try {
  9. //打印返回值信息
  10. System.out.print(ft.get());
  11. } catch (Exception e) {
  12. e.printStackTrace();
  13. }
  14. }
  15. }

1700523-20190614180744171-1477358173.png

4.使用线程池例如用Executor框架

  1. public class ThreadPoolTest {
  2. public static void main(String[] args) {
  3. //创建一个具有10条线程的线程池
  4. ExecutorService exs=Executors.newFixedThreadPool(10);
  5. for(int i=0;i<10;i++){
  6. //匿名内部类来重写run函数,来创建十个线程
  7. exs.execute(new Runnable(){
  8. public void run(){
  9. System.out.println("线程名为:"+Thread.currentThread().getName());
  10. }
  11. });
  12. }
  13. //关闭线程池
  14. exs.shutdown();

运行结果:

1700523-20190614181231585-1054929940.png

ThreadPoolExecutor的参数详解:

1700523-20190614182143719-953018147.png

ThreadPoolExecutor的创建:

  1. //尽量保证:任务数不要超过最大线程数+阻塞队列的长度
  2. ThreadPoolExecutor tp=new ThreadPoolExecutor(5, 10, 5, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>(10));
  3. for(int i=0;i<20;i++){
  4. tp.execute(new Runnable(){
  5. @Override
  6. public void run() {
  7. System.out.println("线程名为:"+Thread.currentThread().getName());
  8. try {
  9. Thread.sleep(1500);
  10. } catch (InterruptedException e) {
  11. e.printStackTrace();
  12. }
  13. }
  14. });
  15. }
  16. tp.shutdown();
  17. }

两个例子:

第一个例子,两个线程交替循环多次:

  1. public class FatherAndSon {
  2. boolean flag=true;
  3. public void son(){
  4. synchronized (this) {
  5. if(!flag){
  6. try {
  7. this.wait();
  8. } catch (Exception e) {
  9. e.printStackTrace();
  10. }
  11. }
  12. for(int i=1;i<=3;i++){
  13. System.out.println("儿子线程"+i);
  14. }
  15. flag=false;
  16. this.notify();
  17. }
  18. }
  19. public void father(){
  20. synchronized (this) {
  21. if(flag){
  22. try {
  23. this.wait();
  24. } catch (Exception e) {
  25. e.printStackTrace();
  26. }
  27. }
  28. for(int i=1;i<=5;i++){
  29. System.out.println("父线程"+i);
  30. }
  31. flag=true;
  32. this.notify();
  33. }
  34. }
  35. }

测试类及其结果:

  1. public class TestFather {
  2. public static void main(String[] args) {
  3. final FatherAndSon fa=new FatherAndSon();
  4. Thread th=new Thread(new Runnable(){
  5. @Override
  6. public void run() {
  7. for(int i=0;i<10;i++){
  8. fa.son();
  9. }
  10. }
  11. });
  12. th.start();
  13. for(int i=0;i<10;i++){
  14. fa.father();
  15. }
  16. }
  17. }

1700523-20190614183945296-965227190.png

第二个例子:循环打印三个汉字

  1. public class PrintlnWorlds implements Runnable{
  2. private String worlds;//要打印的字符
  3. private Object previous;//上一个对象
  4. private Object current;//当前对象
  5. public PrintlnWorlds(String worlds,Object previous,Object current){
  6. super();
  7. this.worlds=worlds;
  8. this.previous=previous;
  9. this.current=current;
  10. }
  11. @Override
  12. public void run() {
  13. for(int i=0;i<10;i++){
  14. synchronized (previous) {
  15. synchronized (current) {
  16. current.notify();
  17. System.out.print(worlds);
  18. }
  19. try {
  20. previous.wait();
  21. } catch (Exception e) {
  22. e.printStackTrace();
  23. }
  24. }
  25. }
  26. }
  27. }

测试类及运行结果:

  1. public class TestPrintln {
  2. public static void main(String[] args) {
  3. Object a=new Object();
  4. Object b=new Object();
  5. Object c =new Object();
  6. PrintlnWorlds p1=new PrintlnWorlds("我",c,a);
  7. PrintlnWorlds p2=new PrintlnWorlds("爱",a,b);
  8. PrintlnWorlds p3=new PrintlnWorlds("你",b,c);
  9. Thread t1=new Thread(p1);
  10. Thread t2=new Thread(p2);
  11. Thread t3=new Thread(p3);
  12. t1.start();
  13. try {
  14. Thread.sleep(1000);
  15. } catch (InterruptedException e) {
  16. e.printStackTrace();
  17. }
  18. t2.start();
  19. try {
  20. Thread.sleep(1000);
  21. } catch (InterruptedException e) {
  22. e.printStackTrace();
  23. }
  24. t3.start();
  25. }
  26. }

1700523-20190614184802568-1058962986.png

转载于:https://www.cnblogs.com/TFE-HardView/p/11014902.html

发表评论

表情:
评论列表 (有 0 条评论,489人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Thread线

    目录 需要笔记的可以关注私聊我发给你 Thread 线程的创建方式 方式一(继承Thread类方式) 方式二(实现Runnable方式) 方式三(实现Callab

    相关 Android Thread线

           相信大家对线程肯定不陌生,我们在编程的过程中经常要用到线程,Android提供了多线程支持,在Android程序中,VM采用抢占式调度模式对线程进行调度(基于线程

    相关 线--继承Thread

    首先继承Thread类,然后重写Thread类的run()方法。 Thread类的子类的对象调用start()方法,然后虚拟机就会调用该线程的run()方法。 当程序执行到

    相关 线Thread

    -------------------- \\java中的线程分类 User Thread 用户线程 运行在前台,执行具体任务: 比如程序的主线程、连接网络的子线程等等

    相关 线-Thread

    线程 线程和进程的区别: ①线程是程序中一个单一的顺序控制流程。进程内一个相对独立的、可调度的执行单元,是系统独立调度和分派CPU的基本单位指运行中的程序的调度单位。