• 自学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()方法。

  • 相关阅读:
    摊牌了……开始入坑硬件开发……Arduion点亮oled小屏
    最后的晚餐——dubbo其他剩余高级知识点分享
    dubbo的负载均衡以及配置方式补充
    dubbo知识点之管理工具dubbo-admin分享
    could not be translated. Either rewrite the query in a form that can be translated, or switch to client evaluation explicitly by inserting a call to 'AsEnumerable',
    netcore 后台任务 指定每天某一时间执行任务
    C# 线程Timer的Change
    EF 取值时出错: Specified cast is not valid
    C# 比较两个数据的不同
    c# json数据解析——将字符串json格式数据转换成对象或实体类
  • 原文地址:https://www.cnblogs.com/lovelifeloveme/p/3186763.html
Copyright © 2020-2023  润新知