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();
            }
        }
    }
    

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

posted @ 2015-05-02 12:13  cjt1991  阅读(160)  评论(0编辑  收藏  举报