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

    java对线程的支持
    java.lang
    class Threadinterface Runnable run()方法
    线程的创建和启动
    创建
    Thread()
    Thread(String name)
    Thread(Runnable target)
    Thread(Runnable target,String name)
    线程常用方法
    void start() 启动线程
    static void sleep(long millis) 线程休眠
    static void sleep(long millis,int nanos)
    void join() 使其他线程等待当前线程终止
    void join(long millis)
    void join(long millis,int nanos)
    static void yield() 当前运行线程释放处理器资源
    static Thread currentThread() 返回当前运行的线程引用
    如何停止线程

     1 package com.imooc.test;
     2 
     3 public class Actor extends Thread {
     4     public void run(){
     5         System.out.println(getName()+"是一个演员!");
     6         int count = 0;
     7         boolean keepRunning = true;
     8         while(keepRunning){
     9             System.out.println(getName()+"登台演出:" + (++count));
    10             
    11             if(count==100){
    12                 keepRunning = false;
    13             }
    14             
    15             if(count%10==0){
    16                 try {
    17                     Thread.sleep(1000);
    18                 } catch (InterruptedException e) {
    19                     e.printStackTrace();
    20                 }
    21             }
    22         }
    23         System.out.println(getName()+"的演出结束了!");
    24     }
    25     
    26     public static void main(String[] args) {
    27         Thread actor = new Actor();
    28         actor.setName("Mr.Thread");
    29         
    30         actor.start();
    31         
    32         Thread actressThread = new Thread(new Actress(),"Ms.Runnable");
    33         
    34         actressThread.start();
    35     }
    36 }    
    37 
    38 class Actress implements Runnable{
    39 
    40     @Override
    41     public void run() {
    42         System.out.println(Thread.currentThread().getName()+"是一个演员!");
    43         int count = 0;
    44         boolean keepRunning = true;
    45         while(keepRunning){
    46             System.out.println(Thread.currentThread().getName()+"登台演出:" + (++count));
    47             
    48             if(count==100){
    49                 keepRunning = false;
    50             }
    51             
    52             if(count%10==0){
    53                 try {
    54                     Thread.sleep(1000);
    55                 } catch (InterruptedException e) {
    56                     e.printStackTrace();
    57                 }
    58             }
    59         }
    60         System.out.println(Thread.currentThread().getName()+"的演出结束了!");        
    61     }
    62     
    63 }
  • 相关阅读:
    POJ 2752 Seek the Name, Seek the Fame
    POJ 2406 Power Strings
    KMP 算法总结
    SGU 275 To xor or not to xor
    hihocoder 1196 高斯消元.二
    hihoCoder 1195 高斯消元.一
    UvaLive 5026 Building Roads
    HDU 2196 computer
    Notions of Flow Networks and Flows
    C/C++代码中的笔误
  • 原文地址:https://www.cnblogs.com/zhuzhuqwa/p/6337118.html
Copyright © 2011-2022 走看看