• Java多线程加强


    一、传统多线程

    public void start()

    Causes this thread to begin execution; the Java Virtual Machine calls the run method of this thread.It is never legal to start a thread more than once。

    public void run()

    If this thread was constructed using a separate Runnable run object, then that Runnable object's run method is called; otherwise, this method does nothing and returns. Subclasses of Thread should override this method.

     /* What will be run. */
    private Runnable target; 
    public void run() {
           if (target != null) {
               target.run();
           }
     }

    public Thread(Runnable target) {
         init(null, target, "Thread-" + nextThreadNum(), 0);
     }

    创建线程的两种方式:

    //1.创建线程的方式一
            Thread thread1 = new Thread(){
                @Override
                public void run() {
                    while(true){
                        System.out.println(Thread.currentThread().getName());
                        try {
                            Thread.sleep(1000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            };
            thread1.start();
            //2.创建线程的方式二
            Thread thread2 = new Thread(new Runnable(){//更加体现面向对象的思想,将线程需要运行的代码装在一个Runnable对象中。
                @Override
                public void run() {
                    while(true){
                        System.out.println(Thread.currentThread().getName());
                        try {
                            Thread.sleep(1000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }});
            thread2.start();

    思考:

    new Thread(new Runnable(){ //如果Thread的子类没有覆盖run方法,则会调用父类的run方法,父类的run方法会调用target对象的run方法
                @Override
                public void run() {
                    while(true){
                        System.out.println("runnable:"+Thread.currentThread().getName());
                        try {
                            Thread.sleep(1000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }){
                public void run() {
                    while(true){
                        System.out.println("thread:"+Thread.currentThread().getName());
                        try {
                            Thread.sleep(1000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                };
            }.start();

    线程的互斥:(要让互斥访问代码块,要用同一个锁对象)

    线程安全问题可以用银行转账来解释。

        class OutPutter{
            public static synchronized void display1(String msg){//静态方法要同步也需要一个锁对象和它关联,静态方法运行时有个对象和它关联,那么这个对象是什么呢?类的字节码在内存中也是一个对象,静态方法运行时还没有类对象,但类的字节码对象已经在内存中了。
                int len = msg.length();
                for(int i = 0 ; i < len ; i++){
                    System.out.print(msg.charAt(i));
                }
                System.out.println();
            }
            public void display(String msg){
                int len = msg.length();
                synchronized (OutPutter.class) {
                    for(int i = 0 ; i < len ; i++){
                        System.out.print(msg.charAt(i));
                    }
                    System.out.println();
                }
            }
        }
        
  • 相关阅读:
    VirtualBox不显示64bit版本的iso
    学习和参考资料
    神经网络和机器学习资料整理
    动态空间释放时的错误操作引起的运行时错误
    WIN7 X64的运行命令窗口
    vs2010中的ADO控件及绑定控件
    AdventureWorks2012.mdf的使用
    VS2008/2010 都不能使用Access2010数据库
    WIN7 64位操作系统 无法找到Access驱动
    如何在VS2010的VC++ 基于对话框的MFC程序中添加菜单
  • 原文地址:https://www.cnblogs.com/java-cjt/p/4471794.html
Copyright © 2020-2023  润新知