zoukankan      html  css  js  c++  java
  • 最简单的java多线程代码(重写thread或者runnable的run方法)

    http://blog.csdn.net/testcs_dn/article/details/42526549

    java线程使用示例——最简单的线程

    线程使用示例一:

     

    [java] view plain copy
     
    1. public class ThreadTest {  
    2.   
    3.     public static void main(String[] args) {  
    4.         //线程使用示例一:  
    5.         new Thread() {  
    6.             public void run() {  
    7.                 while (true) {  
    8.                     try {  
    9.                         System.out.println("线程输出");  
    10.                           
    11.                         //休眠两秒  
    12.                         Thread.sleep(2 * 1000);  
    13.                     } catch (InterruptedException e) {  
    14.                         e.printStackTrace();  
    15.                     }  
    16.                 }  
    17.             };  
    18.         }.start();  
    19.     }  
    20.   
    21. }  

    线程使用示例二:

    [java] view plain copy
     
    1. //线程使用示例二:  
    2. Thread t1 = new Thread(new Runnable(){  
    3.     public void run(){  
    4.         System.out.println("Mythread 线程t1");  
    5.         while(true){  
    6.             Singleton3 single3 = Singleton3.getInstance(null);  
    7.             System.out.println("线程t1 >> " + single3.toString());  
    8.             try {  
    9.                 Thread.sleep(2000);  
    10.             } catch (InterruptedException e) {  
    11.                 // TODO Auto-generated catch block  
    12.                 e.printStackTrace();  
    13.             }  
    14.         }  
    15.           
    16.     }  
    17. });  
    18. t1.start();  
  • 相关阅读:
    [BZOJ]1040: [ZJOI2008]骑士
    [BZOJ]1177: [Apio2009]Oil
    【luogu3384】【模板】树链剖分
    【NOIP2012TG】solution
    【NOIP2014TG】solution
    【NOIP2016TG】solution
    【NOIP2015TG】solution
    【NOIP2016】【LCA】【树上差分】【史诗级难度】天天爱跑步
    【网络流】【BZOJ1221】【HNOI2001】软件开发
    【网络流】【BZOJ1061】【NOI2008】志愿者招募
  • 原文地址:https://www.cnblogs.com/snailmanlilin/p/7931337.html
Copyright © 2011-2022 走看看