首页 > 其他分享 >一) Thread 两种实现方式

一) Thread 两种实现方式

时间:2023-06-01 16:36:25浏览次数:34  
标签:两种 Thread 方式 System dead thread2 thread1 println

https://docs.oracle.com/javase/specs/jls/se7/html/jls-17.html#jls-17.1

https://docs.oracle.com/javase/specs/jls/se7/html/jls-14.html#jls-14.19

http://www.nakov.com/inetjava/lectures/part-1-sockets/InetJava-1.3-Multithreading.html

http://www.uml-diagrams.org/examples/java-6-thread-state-machine-diagram-example.html

 

 

Thread /θrɛd/  A thread of something such as liquid, light, or colour is a long thin line or piece of it. 

process ['prəʊses] a series of actions that are done in order to achieve a particular result.

单线程:execute a single statement or expression at a time

多线程:execute code independently at once

 

 

Java中如何创建一个Thread?

The only way for a user to create a thread is to create an object of java.lang.Thread

 

方式一:继承Thread 类 ,重写 run 方法

public class ThreadDemo {

    public static void main(String args[]) {
        MyThread thread1 = new MyThread("thread1: ");
        MyThread thread2 = new MyThread("thread2: ");
        thread1.start();
        thread2.start();
        boolean theySayThread1IsAlive = true;
        boolean theySayThread2IsAlive = true;
        do {
            if (theySayThread1IsAlive && !thread1.isAlive()) {
                theySayThread1IsAlive = false;
                System.out.println("Thread 1 is dead.");
            }
            if (theySayThread2IsAlive && !thread2.isAlive()) {
                theySayThread2IsAlive = false;
                System.out.println("Thread 2 is dead.");
            }
        } while (theySayThread1IsAlive || theySayThread2IsAlive);
    }

}

class MyThread extends Thread {
    static String message[] = { "Java", "is", "a", "programming", "language." };

    public MyThread(String id) {
        super(id);
    }

    public void run() {
        String name = getName();
        for (int i = 0; i < message.length; i++) {
            randomWait();
            System.out.println(name + message[i]);
        }
    }

    void randomWait() {
        try {
            sleep((long) (3000 * Math.random()));
        } catch (InterruptedException e) {
            System.out.println("Interrupted!");
        }
    }
}

 

thread1: Java
thread2: Java
thread1: is
thread1: a
thread2: is
thread1: programming
thread2: a
thread2: programming
thread2: language.
Thread 2 is dead.
thread1: language.
Thread 1 is dead.

 

方式二:实现 java.lang.Runnable 接口 ,然后构造Thread

public class ThreadDemo {

    public static void main(String args[]) {
        Thread thread1 = new Thread(new MyClass("thread1: "));
        Thread thread2 = new Thread(new MyClass("thread2: "));
        thread1.start();
        thread2.start();
        boolean theySayThread1IsAlive = true;
        boolean theySayThread2IsAlive = true;
        do {
            if (theySayThread1IsAlive && !thread1.isAlive()) {
                theySayThread1IsAlive = false;
                System.out.println("Thread 1 is dead.");
            }
            if (theySayThread2IsAlive && !thread2.isAlive()) {
                theySayThread2IsAlive = false;
                System.out.println("Thread 2 is dead.");
            }
        } while (theySayThread1IsAlive || theySayThread2IsAlive);
    }

}

class MyClass implements Runnable {
    static String message[] = { "Java", "is", "a", "programming", "language." };

    private String id;

    public MyClass(String id) {
        this.id = id;
    }

    public void run() {
        for (int i = 0; i < message.length; i++) {
            randomWait();
            System.out.println(this.id + message[i]);
        }
    }

    void randomWait() {
        try {
            Thread.sleep((long) (3000 * Math.random()));
        } catch (InterruptedException e) {
            System.out.println("Interrupted!");
        }
    }
}

 

thread1: Java
thread1: is
thread2: Java
thread1: a
thread1: programming
thread2: is
thread2: a
thread2: programming
thread1: language.
Thread 1 is dead.
thread2: language.
Thread 2 is dead.

 

 

 

 

 

 

 

标签:两种,Thread,方式,System,dead,thread2,thread1,println
From: https://www.cnblogs.com/zno2/p/6026260.html

相关文章

  • Mybatis的五种分页方式详解
     第一种:LIMIT关键字1,mapper代码select*fromtb_userlimit#{pageNo},#{pageSize}2,业务层直接调用publicListfindByPageInfo(PageInfoinfo){returnuserMapper.selectByPageInfo(info);}3,优点灵活性高,可优化空间大mysql分页语句优化4,缺点实现复杂。 第......
  • linux Capabiltiy 示例——以前只有root和普通用户两种权限,root的权限太大了,现在有了c
    Capabiltiy示例Capability的设定和清除下面的示例程序给当前的进程设定Capability,最后我们清除掉所设置的Capability,源代码如下:#include<stdio.h>#include<stdlib.h>#include<string.h>#include<sys/types.h>#include<unistd.h>#include<sys/capability.h>exter......
  • FTP搭建及内网穿透(Windows环境)、两种(主动、被动)模式
    有两种方法一、使用IIS搭建FTP需要注意的是被动端口设置完后要重启一下【MicrosoftFTPService】服务才能生效Windows系统IIS搭建FTP服务如何配置被动端口?1、打开IIS管理器,点击左侧的服务器,进入服务器设置主页找到 FTP功能 中的 FTP防火墙支持 点击进入2、在数据通道端......
  • Android HandlerThread详解
    概述AndroidHandlerThread使用,自带Looper消息循环的快捷类。详细原文地址:AndroidHandlerThread详解AndroidHandlerThread详解简书一、准备工作开发环境:jdk1.8EclipseLunaServiceRelease1(4.4.1)运行环境:华为荣耀6(Android4.4)、华为p9(Android7.0)实现功能:Andr......
  • JavaScript中几种 获取元素的方式
    1.根据id获取元素document.getElementById("id属性的值");2.根据标签名字获取元素document.getElementsByTagName("标签的名字");3.根据name属性的值获取元素document.getElementsByName("name属性的值");4.根据类样式的名字获取元素document.getElementsByClassName("类样式的名......
  • CSS的五种定位方式【哪一种脱离文档流】
    元素自身居中(非内容)块级元素居中:margin:0auto;行内元素和行内块元素:给上级元素添加text-align:center; 定位方式包括:静态定位相对定位绝对定位固定定位浮动定位静态定位(文档流定位)格式:position:static;(默认的定位方式)......
  • ThreadLocal 详解【并发容器】
    ThreadLocal是什么?有哪些使用场景?ThreadLocal是一个本地线程副本变量工具类,在每个线程中都创建了一个ThreadLocalMap对象,简单说ThreadLocal就是一种以空间换时间的做法,每个线程可以访问自己内部ThreadLocalMap对象内的value。通过这种方式,避免资源在多线程间共享。原理:......
  • Arrays:自定义排序规则的方式二
          ......
  • Spring加载Bean有哪些方式
    转载:https://www.bilibili.com/video/BV1AL411B7Zp/?vd_source=46d50b5d646b50dcb2a208d3946b1598......
  • Arrays类:自定义排序规则的方式一
         ......