zoukankan      html  css  js  c++  java
  • java线程控制方法

    一、中断线程


    1.Thread.sleep()
    让线程进入睡眠状态,放弃CPU的占用暂停若干毫秒
    使用方法:

    public class runable implements Runnable {
    	@Override
    	public void run() {
    		for(int i=1;i<100;i++){
    			System.out.println("first Runnable——>"+i);
    			try {
    				Thread.sleep(200);//设置睡眠时间为200毫秒,
    			} catch (InterruptedException e) {
    				// TODO Auto-generated catch block
    				e.printStackTrace();
    			}
    		}
    	}
    }
    

      

    2.Thread.yidld()

    让线程放弃CPU的占用,但是继续抢占CPU


    二、设置线程的优先级
    默认优先级是5,最大优先级是10,最小优先级是1
    1.getPriority()
    得到当前进程的优先级
    2.setPriority()
    设置当前线程的优先级
    使用方法:

    public class main {
    
    	public static void main(String[] args) throws InterruptedException {
    		//实现接口
    		runable ra=new runable();
    		//生成Thread对象,并将接口对象作为参数
    		Thread t=new Thread(ra);
    		//sleep()方法使用
    		t.sleep(200);
    		//得到当前优先级
    		int temp=t.getPriority();
    		System.out.println("默认优先级:"+temp);
    		//设置最高优先级并输出
    		t.setPriority(Thread.MAX_PRIORITY);
    		temp=t.getPriority();
    		System.out.println("最高优先级:"+temp);
    		//设置最低优先级,并输出
    		t.setPriority(Thread.MIN_PRIORITY);
    		temp=t.getPriority();
    		System.out.println("最低优先级:"+temp);
    	}
    }
    

      

  • 相关阅读:
    test
    设置section的距离
    UISearchBar的应用
    Centos6.6 系统优化
    Linux系统之间文件传输 scp 命令
    MySQL 数据库安装
    AWS中国EC2 公网IP登录免pemKEY修改shh 配置文件
    Ubuntu 16.04 Chrome浏览器安装flash player插件
    Centos6.6 yum源更新
    qume-kvm 命令管理
  • 原文地址:https://www.cnblogs.com/zxxiaoxia/p/4167202.html
Copyright © 2011-2022 走看看