多线程基础

来源:互联网 发布:安徽省软件评测 编辑:程序博客网 时间:2024/06/06 17:41

在jdk中有个位于java.lang命名空间下的Thread类,下面先写一段程序,来分析一下。

    public static void main(String[]args){
        Thread thread=new Thread();
        thread.start();

    }

通过查看java.lang.Thread的源码:

所以上面的代码段其实并没有执行什么有意义的事情。那么如何去实现一个多线程的代码呢?

方法1:

通过子类重写父类的runnable来改变规则。

代码如下:

        

Public static void main(String[] args){

   Thread thread=new Thread(){

  Public void run(){

System.out.println(“doSomeThing”);

}

}

thread.start();

}

方法2:

通过Java.lang.Thread的源码分析,可以看出必须要使target引用一个实际的对象。先看看target的定义:

那么如何给这个target复制呢?从java.lang.Thread的源码中看到:


 

   /**

    * Initializes a Thread.

    *

    * @param g the Thread group

    * @param target the object whose run() method gets called

    * @param name the name of the new Thread

    * @param stackSize the desired stack size for the new thread, or

    *        zero to indicate that this parameter is to be ignored.

    * @param acc the AccessControlContext to inherit, or

    *            AccessController.getContext() if null

    */

   privatevoidinit(ThreadGroup g, Runnabletarget, Stringname,

                     longstackSize, AccessControlContextacc) {

       if (name ==null) {

           thrownew NullPointerException("name cannot be null");

       }

 

       this.name =name.toCharArray();

 

       Thread parent = currentThread();

       SecurityManager security = System.getSecurityManager();

       if (g ==null) {

           /* Determine if it's an applet or not */

 

           /* If there is a security manager, ask the security manager

              what to do. */

           if (security !=null) {

               g =security.getThreadGroup();

           }

 

           /* If the security doesn't have a strong opinion of the matter

              use the parent thread group. */

           if (g ==null) {

               g =parent.getThreadGroup();

           }

       }

 

       /* checkAccess regardless of whether or not threadgroup is

          explicitly passed in. */

       g.checkAccess();

 

       /*

        * Do we have the required permissions?

        */

       if (security !=null) {

           if (isCCLOverridden(getClass())) {

               security.checkPermission(SUBCLASS_IMPLEMENTATION_PERMISSION);

           }

       }

 

       g.addUnstarted();

 

       this.group =g;

       this.daemon =parent.isDaemon();

       this.priority =parent.getPriority();

       if (security ==null ||isCCLOverridden(parent.getClass()))

           this.contextClassLoader =parent.getContextClassLoader();

       else

           this.contextClassLoader =parent.contextClassLoader;

       this.inheritedAccessControlContext =

               acc !=null ?acc : AccessController.getContext();

       this.target = target;

       setPriority(priority);

       if (parent.inheritableThreadLocals != null)

           this.inheritableThreadLocals =

               ThreadLocal.createInheritedMap(parent.inheritableThreadLocals);

       /* Stash the specified stack size in case the VM cares */

       this.stackSize =stackSize;

 

       /* Set thread ID */

       tid =nextThreadID();

    }

代码如下:

  publicstaticvoidmain(String[] args){

     Thread thread=new Thread(new Runnable(){

        @Override

        publicvoid run(){

           System.out.println("doSomeThing");

        }

     });

      Thread.start();

   }

 

思考:

如果两种方式同时使用了,那么会怎么执行呢?代码如下:

     publicstaticvoid main(String[]args){

        Thread thread=newThread(new Runnable(){

           publicvoid run(){

              System.out.println("执行了runnable中的方法!");

           }

        }){

           publicvoid run(){

              System.out.println("执行了方法重写!");

           }

        };

      }

其实从面向对象的思想来看,该类执行时应该先执行本类中的run方法,而不执行java.lang.Thread类中的run方法了。




0 0
原创粉丝点击