zoukankan      html  css  js  c++  java
  • 创建线程方法

    线程:是程序执行流的最小单元,包括就绪、阻塞和运行三种基本状态。

    1. 继承 Thread 类,然后调用 start 方法。

    package com.oracle.demo01;
    
    public class MyThread  extends Thread{
        public void run(){
    //        线程
            System.out.println("线程名"+getName());
        for(int i=0;i<20;i++){
            System.out.println("MyThread-"+i);
        }
    }
    }
    package com.oracle.demo01;
    
    import java.util.concurrent.ThreadPoolExecutor;
    
    public class demo01 {
        
        public static void main(String[] args) {
            Thread th=Thread.currentThread();
            System.out.println(th.getName());
    ////        获取当前线程对象的名称
    //        Thread.currentThread().getName();
    //        创建新线程*(放在上面)
            MyThread mythread=new MyThread();
    //        开启线程、
            mythread.start();
            for(int i=0;i<20;i++){
                System.out.println("main-"+i);
            }
            
        }
    
    }

    2.实现 Runnable 接口的 run 方法, 然后再用 Thread 类包裹后,调用 start 方法。

    package com.oracle.demo02;
    
    public class MyRunnable implements Runnable {
    
        @Override
        public void run() {
    for(int i=0;i<20;i++){
        System.out.println("thread-0"+i);
    }        
        }
    
    }
    package com.oracle.demo02;

    public class demo01 {
        public static void main(String[] args) {
    //        创建线程任务对象
            MyRunnable mr=new MyRunnable();
    //        创建thread对象
            Thread th=new Thread(mr);
            th.start();
            for(int i=0;i<20;i++){
                System.out.println("main-"+i);
            }
            
        }

    3.线程池

    package com.oracle.demo03;
    
    public class MyRun implements Runnable{
        public void run(){
            String name=Thread.currentThread().getName();
            for(int i=0;i<20;i++){
                System.out.println(name+i);
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    
    }
    package com.oracle.demo01;
    
    public class MyThread  extends Thread{
        public void run(){
    //        线程
            System.out.println("线程名"+getName());
        for(int i=0;i<20;i++){
            System.out.println("MyThread-"+i);
        }
    }
    }
    public class demo02 {
    public static void main(String[] args) {
    //    用线程池的方法完成线程任务
    //    从线程池工厂中获取线程池对象
        ExecutorService es=Executors.newFixedThreadPool(2);
    //    创建线程任务对象
        MyRun mr=new MyRun();
    //    让线程池自主选择一条线程执行线程任务
        es.submit(mr);
        es.submit(mr);
        es.submit(mr);
    
    //    关闭线程池
        es.shutdown();
    }
    }
  • 相关阅读:
    Optional int parameter 'resourceState' is present but cannot be translated into a null value
    创建第一个react项目
    后台接收参数报错 Required String parameter 'id' is not present
    sql查询条件参数为空
    斐波那契数列
    Map获取key值
    Java8之集合排序
    Android学习笔记(4)----Rendering Problems(The graphics preview in the layout editor may not be accurate)
    LeetCode赛题395----Longest Substring with At Least K Repeating Characters
    LeetCode赛题394----Decode String
  • 原文地址:https://www.cnblogs.com/-lwl/p/11100908.html
Copyright © 2011-2022 走看看