zoukankan      html  css  js  c++  java
  • 线程中的Thread类中的方法

    /**
    * 线程中常用的方法
    * 1.start():启动线程,运行run()方法
    * 2.run():是Thread类重写的方法,将线程需要执行的操作写在此方法中、
    * 3.currentThread():返回当前线程
    * 4.setName():设置线程名
    * 5.getName():获取线程名
    * 6.yield():线程礼让
    * 7.join():线程抢占
    * 8.stop():强制结束线程
    * 9.sleep(时间):睡眠
    * 参数
    * MAX_PRIORITY:10
    * MIN_PRIORITY:1
    * NORM__PRIORITY:5 默认
    *
    * 设置与获取线程的优先级
    * setPriority(XXX_PRIORITY)
    * getPriority()
    * @Author: ITYW
    * @Create 2020 - 10 - 25 - 22:00
    */
    public class ThreadMethod {
    public static void main(String[] args) {
    MyThread myThread = new MyThread();
    myThread.setName("MyThread");
    Thread.currentThread().setName("main");
    /* myThread.setPriority(Thread.MAX_PRIORITY);
    Thread.currentThread().setPriority(Thread.MIN_PRIORITY);*/
    myThread.start();

    for (int i = 0; i <= 10000; i++) {
    /* try {
    Thread.sleep(100);
    } catch (InterruptedException e) {
    e.printStackTrace();
    }*/
    System.out.println(Thread.currentThread().getName()+"main"+i);
    /*if (i == 10){
    try {
    myThread.join();
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    }*/
    }
    }
    }
    class MyThread extends Thread{

    @Override
    public void run() {
    for (int i = 0; i <= 1000; i++) {
    /* try {
    sleep(100);
    } catch (InterruptedException e) {
    e.printStackTrace();
    }*/
    System.out.println(Thread.currentThread().getName()+"MyThread"+i);
    if (i %10==0){
    yield();
    }
    }
    }
    }
  • 相关阅读:
    C++——并发编程
    Poco C++——JSON解析
    #转载#我给所有新手程序员的建议
    #笔记# 如何阅读技术类书籍
    笔记:CSS hack的学习与了解…
    【笔记】CSS选择器整理(IE低版本支持性测试)
    呼吸灯效果
    ajax跨域问题-----jsonp
    【转】js里的时间函数集
    grunt与requirejs结合使用
  • 原文地址:https://www.cnblogs.com/ITYW/p/13875928.html
Copyright © 2011-2022 走看看