zoukankan      html  css  js  c++  java
  • 自学java--7

    多线程的实现方式有两种:1.直接继承Thread类。  2.继承Runnable接口,然后调用Thread(Runnable runnable)构造函数,来实例化一个Thread。

    其中常用的方法是第二种方式:不仅可以在得到想要的线程,而且可以继续继承其他的类。 另外通过Runnable接口实例化Thread对象还可以很容易实现资源共享。

    例如:

    import javax.xml.stream.events.StartDocument;
    
    
    public class multithread {
    
        public static void main(String[] args)
        {
            System.out.println("Thread name="+Thread.currentThread().getName());
            Thread thread=new myThread();
            //一个线程只能启动一次
            thread.start();
            /*thread.start();
            thread.start();
            thread.start();*/
            //这种方法无法实现数据共享
            /*
            new myThread().start();
            new myThread().start();
            new myThread().start();
            new myThread().start();
            */
            /*
            //这种方式也不可以实现数据共享
            new Thread(new myRunnable()).start();
            new Thread(new myRunnable()).start();
            new Thread(new myRunnable()).start();
            new Thread(new myRunnable()).start();
            */
            //这种方式可以实现数据共享
            /*
            myRunnable runnable=new myRunnable();
            new Thread(runnable).start();
            new Thread(runnable).start();
            new Thread(runnable).start();
            new Thread(runnable).start();
            */
            //这样也不会共享数据
            /*
            thread th1=new thread();
            Thread thre1=th1.getthread();
            thread th2=new thread();
            Thread thre2=th2.getthread();
            thread th3=new thread();
            Thread thre3=th3.getthread();
            thread th4=new thread();
            Thread thre4=th4.getthread();
            thre1.start();
            thre2.start();
            thre3.start();
            thre4.start();
            */
            //利用内部类可以实现数据共享
            thread th5=new thread();
            Thread thre5=th5.getthread();
            Thread thre6=th5.getthread();
            Thread thre7=th5.getthread();
            thre5.start();
            thre6.start();
            thre7.start();
              
            
        }
    
    }
    
    class myThread extends Thread
    {
        int index=0;
        public void run()
        {
            System.out.println("Thread name="+Thread.currentThread().getName()+" "+index++);
        }
    }
    
    class myRunnable implements Runnable
    {
        int index=0;
        public void run() {
            System.out.println("Thread name="+Thread.currentThread().getName()+" "+index++);
            
        }
    }
    
    class thread
    {
        int index=0;
        private class mythread extends Thread
        {
    
            
            public void run() {
                System.out.println("Thread name="+Thread.currentThread().getName()+" "+index++);
                
    } }
    public Thread getthread() { return new mythread(); } }

    线程同步的实现方式也用两种:1.同步块。 2.同步方法。

    同步块:把数据放在一个synchronized(Object obj){共享代码块}中;其中Object obj是一个对象,该对象是随便定义的任意的一个对象,obj对象主要是用来判断该对象是否加锁。每个对象都有一个监视器,或者叫着锁。

    同步方法:在方法前加上synchronized修饰符即可。同步方法利用的是this所代表的对象的锁。

    每一个class也都有一个锁,是这个class所对应的Class对象的锁。

    假如一个类中同时存在同步块和同步函数,那么为了让两者同步运行可以同步块中的Object对象设为this,这样可以实现同类中的同步块和同步函数的同步。

    wait ,notify, notifyAll三个函数的使用:(注意wait,notify,notifyAll三个方法是Object对象中的方法,而不是Thread中的方法)

    每一个对象除了有一个锁外,还有一个等待队列(wait set),当一个对象刚创建的时候,他的等待队列是空的。

    我们应该在当前线程锁住对象的锁后,去调用该对象的wait方法。(调用顺序是重点),也就是说wait(),notify()方法时,必须要在同步块或者同步方法中,且用的必须是同一个对象的等待队列(同一个类中是调用的this这个对象)。

    其中的suspend方法,resume方法,stop方法已经不建议使用了。

    终止线程的方法:1.在run()方法中设置一个flag变量,来使run()方法停止运行。  2.调用Thread类中的interrupt()方法。

  • 相关阅读:
    多个ROS工作空间常见的问题
    ROS tf(现在都使用tf2了)
    ERROR: cannot launch node of type [teleop/teleop_key]: can't locate node [teleop_key] in package [teleop]
    Linux上静态库和动态库的编译和使用
    【C++】类中this指针的理解
    fatal error: No such file or directory
    g2o使用教程
    如何理解二次型
    <ROS> message_filters 对齐多种传感器数据的时间戳
    linux与C内存管理机制
  • 原文地址:https://www.cnblogs.com/lovelifeloveme/p/3186763.html
Copyright © 2011-2022 走看看