zoukankan      html  css  js  c++  java
  • 多线程(二)Object类方法、线程的操作sleep(),join(),interrupt(),yield()

    四、Object类简介

     Object类是所有类的超类,之所以放在线程部分是因为其方法很多是和线程有关的。比如以下三个:

    wait()方法、wait(long timeout)和wait(long timeout,int naos)

    此外,notify()和notifyAll()分别唤醒在该对象上等待的某个线程和在该对象上等待的所有线程。(某个是哪个?)

    wait()和notify()以及notifyAll()接下来还会在展开详细的讲解。

    getClass()方法,居然有final属性,不可重写(仍可以继承),返回对象所属类的详细信息,例如class java.lang.Object,因此通常和getName()连用,那么结果就会变为java.lang.Object。结果记为A。

    hashCode()方法,返回对象的哈希值,可以重写。 结果记为B。

    toString()方法,对于Object类对象来说,相当于前两种方法的结合,即A@B(Hex)。值得注意的是这里的B是十六进制的,而上面的是十进制的。

    equals()方法,最常用的方法也是最被经常重写的方法,a.equals(b)用于判断a和b是否是同一个对象,不过像String类等已经重写为值相等。再记一次,Object类的相等和同一(即==和equals)是等价的。

    五、线程的休眠、加入、中断和礼让

    1.休眠sleep()方法,前面提到过,需要注意到会抛出InterruptedException异常,因此要相应的处理,比如放入try-catch块中或直接抛出。需要注意的是这是一个静态方法,因此一般都是以Thread.sleep()的方式出现,那么睡眠的线程就是执行该代码的线程。

    2.加入join()方法,A线程对象实例在B线程中调用join()方法,代表线程A执行完再执行B线程。同样InterruptedException。

    class thread implements Runnable
    {
        public void run()
        {
            for(int i=0;i<10;i++)
                System.out.println("Thread"+i);
        }
    }
    public class joint {
    public static void main(String []args) throws InterruptedException
    {
        thread a=new thread();
        Thread t=new Thread(a);
        t.start();
    t.join();
    for(int i=0;i<10;i++) System.out.println("main"+i); } }

    显然会打印t线程,再打印主线程。即并行改为串行。

    join(long mil)方法还可以添加参数,即串行等待若干mil毫秒之后开始并行,即B线程会等一会然后开始并行。

    注意:join()只能再start()方法后面调用。

    3.线程的中断interrupt()

     interrupt()方法其实并不会真正杀死线程,只是将中断标志位设置为true,线程仍将运行下去,例如:

    public class interrupt {
        public static void main(String []args) throws InterruptedException
        {
            thread a=new thread();
            Thread t=new Thread(a);
            t.start();
            t.interrupt();    
            System.out.println(t.getState());
        }
    
    }

    结果输出:

    RUNNABLE
    Thread0
    Thread1
    Thread2
    Thread3
    Thread4
    Thread5
    Thread6
    Thread7
    Thread8
    Thread9

    容易混淆的还有interrupted()方法和isInterrupted()方法,其中前者是静态方法。后者是实例方法,用于判断中断位的状态。

            thread a=new thread();
            Thread t=new Thread(a);
            t.start();
            t.interrupt();    
            System.out.println(t.isInterrupted());
            System.out.println(t.isInterrupted());

    线程正常执行,输出两个ture.

    而前者是静态方法,既然无法使用实例调用,那么只能对当前线程进行操作,作用是返回线程状态,并清除线程的true状态,即是true改为false,是false就不变。

            thread a=new thread();
            Thread t=new Thread(a);
            t.start();
            t.interrupt();    
            Thread.currentThread().interrupt();     //如果没这一句,那就全是false
            System.out.println(t.interrupted());
            System.out.println(t.interrupted());
            System.out.println(t.interrupted());

    输出turefalsefalse,因为 是静态方法,所以对象还是类调用应该没什么不同。如果想测试自己的线程,把interrupted()方法写在该线程的run()方法就好了。

    class thread implements Runnable
    {
        public void run()
        {
            System.out.println(Thread.interrupted());
            System.out.println(Thread.interrupted());
            System.out.println(Thread.interrupted());
    
            for(int i=0;i<3;i++)
                System.out.println("Thread"+i);
        }
    }
    
    
    
        public static void main(String []args) throws InterruptedException
        {
            thread a=new thread();
            Thread t=new Thread(a);
            t.start();
            t.interrupt();    
    
    
        }

    结果相同的。

    既然interrupt()只改变中断位,那么如何实现中断呢?

    class thread extends Thread 
    {
        public void run()
        {
                if(!isInterrupted()) {
            for(int i=0;i<3;i++)
                System.out.println("Thread"+i);
        }
                else
                {
                    System.out.println("Thread"+"interrupt");
                    System.out.println(Thread.interrupted());
    
                }
        }
    }
    
    
    
        public static void main(String []args) throws InterruptedException
        {
            thread a=new thread();
            a.start();
            a.interrupt();    
        }

    输出

    Threadinterrupt
    true

    4.线程的礼让yield()

    当前线程从运行态转入就绪态,让其他优先级相同或者更高的线程有机会获得资源,但仅仅暗示本线程愿意让出资源,并不保证一定会将资源礼让,有可能仍是本线程重新获得资源,不常用,因此不展开详细讲解。

    下面准备同步的部分,新开一篇吧,这样容易找而且感觉上更博速度更快。。。

  • 相关阅读:
    Avizo
    NEWS
    HOWTO
    InstallShield 2012 Spring优惠升级到最新版本(2015.4.30之前)
    Windows系统补丁KB2962872导致InstallShield无法启动(解决方案已更新)
    HOWTO: InstallScript MSI工程取Log
    安装软件列表
    阿里云推荐码 hut29f
    ios 缺少合规证明
    ios开发错误之: Undefined symbols for architecture x86_64
  • 原文地址:https://www.cnblogs.com/lbrs/p/10823013.html
Copyright © 2011-2022 走看看