Java thread(3)

    线程间的调度策略

    通常是选择优先级高的线程,但是若发生以下情况则终止线程的运行:
    1 调用yield 让出对cpu的占用权。

    2 调用sleep

    3 线程由于I/O操作而受阻

    4 更高优先级的线程出现

    5 时间片用完

    线程类的一些相关方法

    isAlive()判断线程的死活、getPriority()得到线程的优先级、setPriority()设置线程的优先级、leep()方法使得线程休眠、yield方法放弃线程对cpu的使用权。

    对于两个线程何时公用一个变量 何时私自拥有自己的变量的比较

    当变量是类的成员变量的时候,这个变量被两个线程共享,如果是局部变量,比如定义在run方法中,这两个线程各自有自己的变量。

 

//情况一:
//两个线程 各自都有私有的变量 因为int变量声明在run方法中 结果会出来20个数
package com.javase.thread;

public class threadTest2 {
    
    public static void main(String[]args){
        Runnable r=new helloThread();
        Thread t1=new Thread(r);
        Thread t2=new Thread(r);
        t1.start();
        t2.start();
        
    }
}
    
    class helloThread implements Runnable{

        
        public void run() {
            int i=0;
            while(true){
                i++;
                try {
                    Thread.sleep((long)Math.random()*2000);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                
                if(i>10)
                    {break;}
                System.out.println("the number is "+i);
            }
        }
        
    }
//情况二:
//int变量声明在类中 是类的成员变量 两个线程会共享着同一个公有的变量 结果会打印出来10个数字
package com.javase.thread;

public class threadTest2 {
    
    public static void main(String[]args){
        Runnable r=new helloThread();
        Thread t1=new Thread(r);
        Thread t2=new Thread(r);
        t1.start();
        t2.start();
        
    }
}
    
    class helloThread implements Runnable{
        int i=0;
        
        public void run() {
            
            while(true){
                i++;
                try {
                    Thread.sleep((long)Math.random()*2000);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                
                if(i>10)
                    {break;}
                System.out.println("the number is "+i);
            }
        }
        
    }
//情况三: //在第一种共享成员变量的情况下 如果生成两个runnable接口(helloThread对象) //则每个线程各自仍然会有自己的变量 不会发生冲突 两个helloThread对象 有各自的变量 结果会出来20个数字
package com.javase.thread;

public class threadTest2 {
    
    public static void main(String[]args){
        Runnable r1=new helloThread();
        Runnable r2=new helloThread();
        Thread t1=new Thread(r1);
        Thread t2=new Thread(r2);
        t1.start();
        t2.start();
        
    }
}
    
    class helloThread implements Runnable{
        int i=0;
        
        public void run() {
            
            while(true){
                i++;
                try {
                    Thread.sleep((long)Math.random()*2000);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                
                if(i>10)
                    {break;}
                System.out.println("the number is "+i);
            }
        }
        
    }

转载于:https://www.cnblogs.com/Goden/p/3836664.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值