文章详情

短信预约-IT技能 免费直播动态提醒

请输入下面的图形验证码

提交验证

短信预约提醒成功

Java多线程基础知识点有哪些

2023-06-25 11:44

关注

这篇文章主要为大家展示了“Java多线程基础知识点有哪些”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“Java多线程基础知识点有哪些”这篇文章吧。

一、线程

什么是线程:

线程是进程的一个实体,是CPU调度和分派的基本单位,它是比进程更小的能独立运行的基本单位。

什么是多线程:

多线程指在单个程序中可以同时运行多个不同的线程执行不同的任务。

二、创建多线程的方式

多线程的创建方式有三种:ThreadRunnableCallable

1、继承Thread类实现多线程

  Thread thread = new Thread() {            @Override            public void run() {                super.run();                while (true) {                    try {                        Thread.sleep(500);                    } catch (InterruptedException e) {                        e.printStackTrace();                    }                    System.out.println("1:" + Thread.currentThread().getName());                    System.out.println("2:" + this.getName());                }            }        };        thread.start();

2、实现Runnable接口方式实现多线程

Thread thread1 = new Thread(new Runnable() {            @Override            public void run() {                while (true) {                    try {                        Thread.sleep(500);                    } catch (InterruptedException e) {                        e.printStackTrace();                    }                    System.out.println("3:" + Thread.currentThread().getName());                }            }        });        thread1.start();

3、Callable接口创建线程

public class CallableTest {    public static void main(String[] args) {        System.out.println("当前线程是:" + Thread.currentThread());        Callable myCallable = new Callable() {            @Override            public Integer call() throws Exception {                int i = 0;                for (; i < 10; i++) {                }                //当前线程                System.out.println("当前线程是:" + Thread.currentThread()                        + ":" + i);                return i;            }        };        FutureTask<Integer> fu = new FutureTask<Integer>(myCallable);        Thread th = new Thread(fu, "callable thread");        th.start();        //得到返回值        try {            System.out.println("返回值是:" + fu.get());        } catch (Exception e) {            e.printStackTrace();        }    }}

当前线程是:Thread[main,5,main]
当前线程是:Thread[callable thread,5,main]:10
返回值是:10

总结:

实现Runnable接口相比继承Thread类有如下优势:

实现Runnable接口和实现Callable接口的区别:

三、线程的生命周期与状态

Java多线程基础知识点有哪些

四、线程的执行顺序

Join线程的运行顺序

原理:

1、定时器

import java.util.Timer;import java.util.TimerTask;public class TraditionalTimerTest {    public static void main(String[] args) {//         new Timer().schedule(new TimerTask() {//             @Override//             public void run() {////                 System.out.println("bombing!");//             }//         },10000);        class MyTimberTask extends TimerTask {            @Override            public void run() {                System.out.println("bombing!");                new Timer().schedule(new MyTimberTask(), 2000);            }        }        new Timer().schedule(new MyTimberTask(), 2000);        int count = 0;        while (true) {            System.out.println(count++);            try {                Thread.sleep(1000);            } catch (InterruptedException e) {                e.printStackTrace();            }        }    }}

0
1
bombing!
2
3
bombing!
4
5
bombing!
6
省略...

2、线程的互斥与同步通信

public class TraditionalThreadSynchronized {    public static void main(String[] args) {        new TraditionalThreadSynchronized().init();    }    private void init() {        final Outputer outputer = new Outputer();        new Thread(new Runnable() {            @Override            public void run() {                while (true) {                    try {                        Thread.sleep(10);                    } catch (InterruptedException e) {                        e.printStackTrace();                    }                    outputer.output("kpioneer");                }            }        }).start();//        new Thread(new Runnable() {//            @Override//            public void run() {////                while (true) {//                    try {//                        Thread.sleep(10);//                    } catch (InterruptedException e) {//                        e.printStackTrace();//                    }//                    outputer.output2("Tom");////                }//            }//        }).start();        new Thread(new Runnable() {            @Override            public void run() {                while (true) {                    try {                        Thread.sleep(10);                    } catch (InterruptedException e) {                        e.printStackTrace();                    }                    outputer.output3("Jack");                }            }        }).start();    }  static   class Outputer {        public void output(String name) {            int len = name.length();            synchronized (Outputer.class) {                for (int i = 0; i < len; i++) {                    System.out.print(name.charAt(i));                }                System.out.println();            }        }        public synchronized void output2(String name) {            int len = name.length();            for (int i = 0; i < len; i++) {                System.out.print(name.charAt(i));            }            System.out.println();        }      public static synchronized void output3(String name) {          int len = name.length();          for (int i = 0; i < len; i++) {              System.out.print(name.charAt(i));          }          System.out.println();      }    }}

3、线程同步通信技术

子线程循环10次,接着主线程循环100,接着又回到子线程循环10次,接着再回到主线程有循环100,如此循环50次,请写出程序。

public class TraditionalThreadCommunication {    public static void main(String[] args) {        final Business business = new Business();        new Thread(new Runnable() {                    @Override                    public void run() {                        for (int i = 1; i <= 50; i++) {                            business.sub(i);                        }                    }                }        ).start();        for (int i = 1; i <= 50; i++) {            business.main(i);        }    }}class Business {    private boolean bShouldSub = true;    public synchronized void sub(int i) {        if(!bShouldSub) {            try {                this.wait();            } catch (InterruptedException e) {                e.printStackTrace();            }        }        for (int j = 1; j <= 10; j++) {            System.out.println("sub thread sequece of " + j + ",loop of " + i);        }        bShouldSub = false;        this.notify();    }    public synchronized void main(int i) {        if(bShouldSub) {            try {                this.wait();            } catch (InterruptedException e) {                e.printStackTrace();            }        }        for (int j = 1; j <=100; j++) {            System.out.println("main thread sequece of " + j + ",loop of " + i);        }        bShouldSub = true;        this.notify();    }}

sub thread sequece of 1,loop of 1
sub thread sequece of 2,loop of 1
sub thread sequece of 3,loop of 1
sub thread sequece of 4,loop of 1
sub thread sequece of 5,loop of 1
sub thread sequece of 6,loop of 1
sub thread sequece of 7,loop of 1
sub thread sequece of 8,loop of 1
sub thread sequece of 9,loop of 1
sub thread sequece of 10,loop of 1
main thread sequece of 1,loop of 1
main thread sequece of 2,loop of 1
main thread sequece of 3,loop of 1
main thread sequece of 4,loop of 1
main thread sequece of 5,loop of 1
main thread sequece of 6,loop of 1
main thread sequece of 7,loop of 1
main thread sequece of 8,loop of 1
main thread sequece of 9,loop of 1
main thread sequece of 10,loop of 1
main thread sequece of 11,loop of 1
省略中间...
main thread sequece of 99,loop of 1
main thread sequece of 100,loop of 1
sub thread sequece of 1,loop of 2
sub thread sequece of 2,loop of 2
sub thread sequece of 3,loop of 2
sub thread sequece of 4,loop of 2
sub thread sequece of 5,loop of 2
sub thread sequece of 6,loop of 2
sub thread sequece of 7,loop of 2
sub thread sequece of 8,loop of 2
sub thread sequece of 9,loop of 2
sub thread sequece of 10,loop of 2
main thread sequece of 1,loop of 2
main thread sequece of 2,loop of 2
main thread sequece of 3,loop of 2
省略...

以上是“Java多线程基础知识点有哪些”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注编程网行业资讯频道!

阅读原文内容投诉

免责声明:

① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。

② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341

软考中级精品资料免费领

  • 历年真题答案解析
  • 备考技巧名师总结
  • 高频考点精准押题
  • 2024年上半年信息系统项目管理师第二批次真题及答案解析(完整版)

    难度     813人已做
    查看
  • 【考后总结】2024年5月26日信息系统项目管理师第2批次考情分析

    难度     354人已做
    查看
  • 【考后总结】2024年5月25日信息系统项目管理师第1批次考情分析

    难度     318人已做
    查看
  • 2024年上半年软考高项第一、二批次真题考点汇总(完整版)

    难度     435人已做
    查看
  • 2024年上半年系统架构设计师考试综合知识真题

    难度     224人已做
    查看

相关文章

发现更多好内容

猜你喜欢

AI推送时光机
位置:首页-资讯-后端开发
咦!没有更多了?去看看其它编程学习网 内容吧
首页课程
资料下载
问答资讯