码迷,mamicode.com
首页 > 编程语言 > 详细

多线程之方法分配

时间:2015-06-09 20:17:12      阅读:132      评论:0      收藏:0      [点我收藏+]

标签:

import java.lang.reflect.Method;


public class Obj {

int obj_i = 11;

Integer obj_int = new Integer(obj_i);

    public void println(String str,boolean b){

    int fun_i = 99;

    Integer fun_int = new Integer(fun_i);

        try {

            System.out.println(str + "start");

            fun_i --;

            obj_i ++;

            if(b){

                Thread.sleep(2000);

                fun_i --;

                obj_i ++;

            }

            System.out.println(str + "end");

            System.out.println(str + "对象>" + this + ">" + System.identityHashCode(this));

            System.out.println(str + "对象>" + obj_i + ">" + System.identityHashCode(obj_i));

            System.out.println(str + "对象>" + obj_int + ">" + System.identityHashCode(obj_int));

            System.out.println(str + "方法>" + fun_i + ">" + System.identityHashCode(fun_i));

            System.out.println(str + "方法>" + fun_int + ">" + System.identityHashCode(fun_int));

            Method[] mds = this.getClass().getMethods();

            for (Method md : mds) {

            if ("println".equals(md.getName())) {

            System.out.println(str + "方法>" + md + ">" + System.identityHashCode(md));

            }

            }

        } catch (InterruptedException e) {

            e.printStackTrace();

        }

    }

}

public class Test {


public static void main(String[] args) {

        final Obj obj = new Obj();

        Thread t3 = new Thread(new Runnable() {


            @Override

            public void run() {

                obj.println("c", true);


            }

        });

        Thread t4 = new Thread(new Runnable() {


            @Override

            public void run() {

            int i = 0;

//                for(int i = 0; i < 20; i++){

                    obj.println("d" + i, false);

//                }


            }

        });

        t3.start();

        t4.start();

        obj.println("main", false);

    }

}

结果

--------------------

mainstart

mainend

main对象>javay.test.Obj@759ebb3d>1973336893

main对象>12>1212899836

main对象>11>1174290147

main方法>98>1289696681

main方法>99>1285044316

main方法>public void javay.test.Obj.println(java.lang.String,boolean)>1607460018

cstart

d0start

d0end

d0对象>javay.test.Obj@759ebb3d>1973336893

d0对象>14>651376380

d0对象>11>1174290147

d0方法>98>1289696681

d0方法>99>672455766

d0方法>public void javay.test.Obj.println(java.lang.String,boolean)>741039500

cend

c对象>javay.test.Obj@759ebb3d>1973336893

c对象>15>1413515514

c对象>11>1174290147

c方法>97>1518828546

c方法>99>1589175178

c方法>public void javay.test.Obj.println(java.lang.String,boolean)>465204793


多线程之方法分配

标签:

原文地址:http://my.oschina.net/u/660460/blog/464756

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!