二、java实现多线程的方式?

mac2022-06-30  26

一、同步?异步?

     下面两幅图解释了同步异步。

       

 

 二、实现多线程的方式

     1.继承Thread

        

package threaddemo; class CreateThreadDemo extends Thread{ /** * 线程执行的任务,执行的代码 */ @Override public void run() { for (int i = 0; i <30 ; i++) { System.out.println("我是线程:"+i+ "名字:"+Thread.currentThread().getName()); } } } /** * @version V1.0 * @description:实现线程的方式 * @author: BingShu * @contact: wx:hsj179540 * @date: 2019年-08月-11日-23 */ public class ThreadDemo { /** * 什么是进程?什么是线程? * 多线程的应用场景? * 多线程的优点? * java实现多线程的方式? * 1.集成Thread类重写run方法 */ public static void main(String[] args) { CreateThreadDemo createThreadDemo=new CreateThreadDemo(); createThreadDemo.start(); for (int i = 0; i <30 ; i++) { System.out.println("我是线程:"+i+ "名字:"+Thread.currentThread().getName()); } } }

 

2.实现runable接口(推荐)

package threaddemo; class CreateThreadDemo2 implements Runnable { /** * 执行线程代码 */ @Override public void run() { for (int i = 0; i < 30; i++) { System.out.println("我是线程:" + i + "名字:" + Thread.currentThread().getName()); } } } /** * @version V1.0 * @description: * @author: BingShu * @contact: wx:hsj179540 * @date: 2019年-08月-11日-23 */ public class ThreadDemo2 { public static void main(String[] args) { CreateThreadDemo2 createThreadDemo2=new CreateThreadDemo2(); new Thread(createThreadDemo2).start(); for (int i = 0; i <30 ; i++) { System.out.println("我是线程:"+i+ "名字:"+Thread.currentThread().getName()); } } }

 

 3.使用内部类实现

package threaddemo; /** * @version V1.0 * @description:使用内部类实现 * @author: BingShu * @contact: wx:hsj179540 * @date: 2019年-08月-11日-23 */ public class ThreadDemo3 { public static void main(String[] args) { Thread thread=new Thread(new Runnable() { @Override public void run() { for (int i = 0; i <30 ; i++) { System.out.println("我是线程:"+i+ "名字:"+Thread.currentThread().getName()); } } }); thread.start(); for (int i = 0; i <30 ; i++) { System.out.println("我是线程:"+i+ "名字:"+Thread.currentThread().getName()); } } }

 

 

三、内部类的使用(补充)

package threaddemo; abstract class Panret { //内部类 abstract void add(); } /** * @version V1.0 * @description:内部类的使用 * @author: BingShu * @contact: wx:hsj179540 * @date: 2019年-08月-11日-23 */ public class InternalClass { public static void main(String[] args) { Panret p = new Panret() { @Override void add() { System.out.println("我是内部类"); } }; //方法调用 p.add(); } }

 

四、常用api的使用

package threaddemo; /** * 常用api * getId() 获取线程Id * getName(); // 获取线程名字 * stop(); //停止 * Thread.sleep(long value); 休眠 */ class CreateThreadApiUse implements Runnable { @Override public void run() { for (int i = 0; i < 30; i++) { System.out.println("线程id:" + Thread.currentThread().getId() + " 线程Name:" + Thread.currentThread().getName()); if(i==5){ //不安全不建议大家使用 Thread.currentThread().stop(); } } } } /** * @version V1.0 * @description: * @author: BingShu * @contact: wx:hsj179540 * @date: 2019年-08月-11日-23 */ public class ThreadApi { public static void main(String[] args) { CreateThreadApiUse createThreadApiUse = new CreateThreadApiUse(); //自定义线程名字 Thread thread = new Thread(createThreadApiUse,"我是线程的名字"); thread.start(); try { //休眠 Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); System.out.println("线程异常"); } } }

 

 

总结:

    1.同步异步?

    2.多线程常用的实现方式?(实现Runable方式推荐,原因:java面向接口编程,实现接口可以多继承,但是直接继承Thread将无法继承        其他的类。)

      还有其他方式实现多线程?请自行科普。

    3.内部类的使用?

    4.Thread常用api

 

有问题欢迎咨询,或者加入我的群:微信 :hsj179540

转载于:https://www.cnblogs.com/bingshu/p/11337390.html

最新回复(0)