多线程入门
发布日期:2022-04-11 08:52:52 浏览次数:13 分类:博客文章

本文共 4400 字,大约阅读时间需要 14 分钟。

一:线程与进程——线程就是正在独立运行的一条执行路径。进程就是正在运行的程序,它是线程的集合。一个进程中一定有一个主线程,它就是主线程main。

二:什么是多线程?——就是为了提高程序的效率。

三:创建线程的方式:①使用继承Thread类的方式创建线程;

                                    ②使用实现Runnable接口的方式创建线程;

                                    ③使用匿名内部类方式;

                                    ④callable

                                    ⑤使用线程池创建线程;

     继承方式:

public class Demo01 extends Thread{@Overridepublic void run() {    for (int i = 0; i <10; i++) {        System.out.println("线程:"+i+",线程名:"+Thread.currentThread().getName());        try {            Thread.sleep(1000);        } catch (InterruptedException e) {            // TODO Auto-generated catch block            e.printStackTrace();        }    }    super.run();}public static void main(String[] args) {    Demo01 t1=new Demo01();    t1.start();        System.out.println("主线程名:"+Thread.currentThread().getName());    for (int i = 0; i < 10; i++) {        System.out.println("main:"+i);    }}}

实现Runnable接口方式:

public class Demo02 implements Runnable {    @Override    public void run() {        for (int i = 0; i <10; i++) {            System.out.println("线程:"+i+",线程名:"+Thread.currentThread().getName());        }    }    public static void main(String[] args) {        Demo02 t1=new Demo02();        Thread thread=new Thread(t1,"子线程");        thread.start();        for (int i = 0; i < 10; i++) {            System.out.println("main线程:"+i);        }    }}

匿名内部类方式:

public class Demo03 {public static void main(String[] args) {    Thread thread = new Thread( new Runnable() {        public void run() {            for (int i = 0; i <10; i++) {                System.out.println("子线程:"+i+",线程名:"+Thread.currentThread().getName());            }        }    });    thread.start();    for (int i = 0; i <10; i++) {        System.out.println("主线程:"+i+",线程名:"+Thread.currentThread().getName());    }}}

四:守护线程和非守护线程——守护线程会随着主线程的销毁而销毁(例如GC线程);非守护线程就是用户创建的线程,与主线程互不影响

public class Demo04 {public static void main(String[] args) {    Thread thread = new Thread( new Runnable() {        public void run() {            for (int i = 0; i <10; i++) {                try {                    Thread.sleep(300);                } catch (InterruptedException e) {                    // TODO Auto-generated catch block                    e.printStackTrace();                }                System.out.println("子线程:"+i+",线程名:"+Thread.currentThread().getName());            }        }    });    thread.setDaemon(true);//将thread设置为守护线程,与主线程一同销毁;    thread.start();    for (int i = 0; i <=4; i++) {        try {            Thread.sleep(300);        } catch (InterruptedException e) {            // TODO Auto-generated catch block            e.printStackTrace();        }        System.out.println("主线程:"+i+",线程名:"+Thread.currentThread().getName());    }    System.out.println("主线程销毁....");}}

 五:多线程的几种状态——新建状态;就绪状态;运行状态;阻塞状态;死亡状态;

六:jion方法——表示当前线程让给join的线程先执行;例如:

public class Demo05 {public static void main(String[] args) {    Thread thread1 = new Thread( new Runnable() {        public void run() {            for (int i = 0; i <10; i++) {                try {                    Thread.sleep(300);                } catch (InterruptedException e) {                    // TODO Auto-generated catch block                    e.printStackTrace();                }                System.out.println("子线程1:"+i);            }        }    });    thread1.start();    Thread thread2 = new Thread( new Runnable() {        public void run() {                        for (int i = 0; i <10; i++) {                try {                    Thread.sleep(300);                } catch (InterruptedException e) {                    // TODO Auto-generated catch block                    e.printStackTrace();                }                System.out.println("子线程2:"+i);            }        }    });    try {        thread1.join();    } catch (InterruptedException e) {        // TODO Auto-generated catch block        e.printStackTrace();    }    thread2.start();    Thread thread3 = new Thread( new Runnable() {        public void run() {            for (int i = 0; i <10; i++) {                try {                    Thread.sleep(300);                } catch (InterruptedException e) {                    // TODO Auto-generated catch block                    e.printStackTrace();                }                System.out.println("子线程3:"+i);            }        }    });    try {        thread2.join();    } catch (InterruptedException e) {        // TODO Auto-generated catch block        e.printStackTrace();    }    thread3.start();}}

 

 

 

转载地址:https://www.cnblogs.com/r1002/p/11045677.html 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:多线程入门-并发编程(Java)
下一篇:多线程例题练手

发表评论

最新留言

网站不错 人气很旺了 加油
[***.192.178.218]2024年04月11日 12时51分32秒