• Java 创建多线程


    Java语言内置了多线程支持。当Java程序启动的时候,实际上是启动了一个JVM进程,然后,JVM启动主线程来执行main()方法。在main()方法中,我们又可以启动其他线程。

    Java 提供了三种创建线程的方法:

    • 通过实现 Runnable 接口;
    • 通过继承 Thread 类本身;
    • 通过 Callable 和 Future 创建线程。

    通过实现 Runnable 接口来创建线程

    class RunnableDemo implements Runnable {
       private Thread t;
       private String threadName;
       
       RunnableDemo( String name) {
          threadName = name;
          System.out.println("Creating " +  threadName );
       }
       
       public void run() {
          System.out.println("Running " +  threadName );
          try {
             for(int i = 4; i > 0; i--) {
                System.out.println("Thread: " + threadName + ", " + i);
                // 让线程睡眠一会
                Thread.sleep(50);
             }
          }catch (InterruptedException e) {
             System.out.println("Thread " +  threadName + " interrupted.");
          }
          System.out.println("Thread " +  threadName + " exiting.");
       }
       
       public void start () {
          System.out.println("Starting " +  threadName );
          if (t == null) {
             t = new Thread (this, threadName);
             t.start ();
          }
       }
    }
     
    public class TestThread {
     
       public static void main(String args[]) {
          RunnableDemo R1 = new RunnableDemo( "Thread-1");
          R1.start();
          
          RunnableDemo R2 = new RunnableDemo( "Thread-2");
          R2.start();
       }   
    }
    View Code

    通过继承Thread来创建线程

    class ThreadDemo extends Thread {
       private Thread t;
       private String threadName;
       
       ThreadDemo( String name) {
          threadName = name;
          System.out.println("Creating " +  threadName );
       }
       
       public void run() {
          System.out.println("Running " +  threadName );
          try {
             for(int i = 4; i > 0; i--) {
                System.out.println("Thread: " + threadName + ", " + i);
                // 让线程睡眠一会
                Thread.sleep(50);
             }
          }catch (InterruptedException e) {
             System.out.println("Thread " +  threadName + " interrupted.");
          }
          System.out.println("Thread " +  threadName + " exiting.");
       }
       
       public void start () {
          System.out.println("Starting " +  threadName );
          if (t == null) {
             t = new Thread (this, threadName);
             t.start ();
          }
       }
    }
     
    public class TestThread {
     
       public static void main(String args[]) {
          ThreadDemo T1 = new ThreadDemo( "Thread-1");
          T1.start();
          
          ThreadDemo T2 = new ThreadDemo( "Thread-2");
          T2.start();
       }   
    }
    View Code

     Thread类方法

    Thread类静态方法

    参考资料

    菜鸟教程

  • 相关阅读:
    SQL语句-用sql语句得到表中所有字段的名字
    SQL语句-判断数据库中是否有这张表
    Java开发-创建第一个项目hello world
    MFC-一个很简单的程序最小化到托盘,带右键菜单带图标
    Everything SDK-基于名称快速定位文件和文件夹。
    NX二次开发-基于PycharmIDE的NXOpen Python开发环境配置
    手动局域网搭建FTP服务器
    FreeCAD二次开发-基于控制台模式FC外部开发
    FreeCAD二次开发-基于PyQT对话框与FC交互的开发
    实例开发-NX二次开发手动截图工具
  • 原文地址:https://www.cnblogs.com/xumaomao/p/12833168.html
Copyright © 2020-2023  润新知