zoukankan      html  css  js  c++  java
  • 线程的强制运行

    在java运行时至少会启动两个线程,一个是main线程,一个是垃圾收集线程。

    在线程操作中,可以使用join()方法让一个线程强制运行,线程强制运行期间,其他线程无法运行,必须等待此线程完成之后才可以继续执行:

    class myThread10 implements Runnable{

        public void run() {
            for(int i=0;i<50;i++){
                System.out.println(Thread.currentThread().getName()+" Runing "+i);
            }
           
        }
       
    }
    public class ThreadJoinDemo {
        public static void main(String[] args) {
            myThread10 m=new myThread10();
            Thread thread=new Thread(m, "Thread v");
            thread.start();
            for(int i=0;i<50;i++){
                if(i>10){
                    try {
                        thread.join();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                System.out.println("Main thread running:"+i);
            }
        }

    }

  • 相关阅读:
    Keep at Most 100 Characters
    Larry and Inversions
    计算指数
    简单题
    重要的话说三遍
    I Love GPLT
    猜数字
    打印沙漏
    多态性(polymorphism),封装性(encapsulation),内聚(cohesion)以及耦合(coupling)的基本概念
    Hibernate面试题
  • 原文地址:https://www.cnblogs.com/vonk/p/3893997.html
Copyright © 2011-2022 走看看