• 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 }
  • 相关阅读:
    LNK1123: 转换到 COFF 期间失败: 文件无效或损坏
    [转载]弹出一个不带地址栏、工具栏的IE非模态窗口
    winform datagridview 如何设置datagridview隔行变色
    C#调用C++的dll存在的问题
    Seafile开源私有云自定义首页Logo图片
    Alpine Linux 安装 lxml Pillow 失败
    Docker容器 MySQL中文乱码解决方案
    JS 查找数组的父节点及祖先节点
    五分钟读懂UML类图(转)
    Ant Design按需加载
  • 原文地址:https://www.cnblogs.com/zhuzhuqwa/p/6337118.html
Copyright © 2020-2023  润新知