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();  
  • 相关阅读:
    多线程(一)高并发和多线程的关系
    spring源码浅析——IOC
    网络爬虫(java)
    数据结构—平衡二叉树
    设计模式—抽象工厂模式
    设计模式—工厂方法模式
    scala(二) Future执行逻辑解读
    java异常处理机制
    为什么覆写equals必须要覆写hashCode?
    Scala对MongoDB的增删改查操作
  • 原文地址:https://www.cnblogs.com/snailmanlilin/p/7931337.html
Copyright © 2011-2022 走看看