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); 
     } 
     } 
    }
    

  • 相关阅读:
    SQL中UNION的使用
    [转]身份证号准确性检测
    shell中if/seq/for/while/until
    shell中数字、字符串、文件比较测试
    shell简介及变量的定义查看撤销
    grep/字符/次数匹配/锚定符/小大括号/wc/tr/cut/sort/uniq
    linux全局和个人配置文件说明
    linux文件的3个时间和7种文件类型
    linux常用配置文件和命令总结
    目录方式扩展swap分区大小
  • 原文地址:https://www.cnblogs.com/xingkongcanghai/p/12807754.html
Copyright © 2011-2022 走看看