zoukankan      html  css  js  c++  java
  • java多线程

    import java.util.*; 
    import java.text.*; 
    public class TestThread3 { 
     public static void main(String args[]) { 
     Runner r1 = new Runner(1); 
     Thread t1 = new Thread(r1); 
     Thread t2 = new Thread(r1); 
     Thread t3 = new Thread(r1); 
     Runner r2 = new Runner(2); 
     Thread t4 = new Thread(r2); 
     Thread t5 = new Thread(r2); 
     Thread t6 = new Thread(r2); 
     Timer timer = new Timer(); 
     Thread t7 = new Thread(timer); 
     t1.start(); 
     t2.start(); 
     t3.start(); 
     t4.start(); 
     t5.start(); 
     t6.start(); 
     t7.start(); 
     } 
    } 
    class Runner implements Runnable { 
     int id; 
     Runner(int id){ 
     this.id = id; 
     } 
     public void run() { 
     int i=0; 
     while( true ){ 
     i++; 
     System.out.println("ID: " + id + " No. " + i); 
     try{ Thread.sleep(300); } catch( InterruptedException 
    e ){} 
     }
    } 
    } 
    class Timer implements Runnable { 
     public void run(){ 
     while(true){ 
     System.out.println( new SimpleDateFormat(). 
    format( new Date())); 
     try{ Thread.sleep(1000); } catch( InterruptedException 
    e ){} 
     } 
     } 
    }
    


    msconfig



    public class TestThreadPriority { 
     public static void main(String args[]) { 
     Thread t1 = new Thread( new Runner(1) ); 
     Thread t2 = new Thread( new Runner(2) ); 
     Thread t3 = new Thread( new Runner(3) ); 
     t1.setPriority( Thread.MIN_PRIORITY ); 
     t2.setPriority( Thread.NORM_PRIORITY ); 
     t3.setPriority( Thread.MAX_PRIORITY ); 
     t1.start(); 
     t2.start(); 
     t3.start(); 
     } 
    }
    class Runner implements Runnable { 
     int id; 
     Runner(int id){ 
     this.id = id; 
     } 
     public void run() { 
     for(int i=0; i<100; i++ ){ 
     if( i % 100 == 0 ) System.out.print("
    "); 
     Thread.currentThread().yield(); 
     System.out.print(id); 
     } 
     } 
    }
    

  • 相关阅读:
    Nginx配置文件详解
    ngrinder 负载均衡脚本开发
    spring boot过滤器FilterRegistrationBean
    Spring boot 拦截器和过滤器
    spring controller中默认转发、forward转发、redirect转发之间的区别
    @RestControllerAdvice作用及原理
    Android APK脱壳--腾讯乐固、360加固一键脱壳
    java 简单xor加密
    facebook 研究
    阿里云docker安装
  • 原文地址:https://www.cnblogs.com/xingkongcanghai/p/12807754.html
Copyright © 2011-2022 走看看