爱程序网

《多线程编程》——创建线程的两种方式

来源: 阅读:

1.目的

         创建线程,即拿到一个线程实例。这个线程实例必须具备开启、等待、唤醒等控制自身生命周期的方法。

 

2.创建Thread线程

           方式一:new Thread()、new Thread(String name)       

 1 /**
 2    *两个构造器也有区别:线程名不同
 3    */
 4 public class Thread implements Runnable {
 5 private char   name[];
 6 
 7    //1.不带参
 8    /*
 9     *Automatically generated names are of the form *"Thread-"+n, where n is an integer
10     */
11    public Thread() {
12         init(null, null, "Thread-" + nextThreadNum(), 0);
13     }
14     private static synchronized int nextThreadNum() {
15         return threadInitNumber++;
16     }
17    //2.带参
18     public Thread(String name) {
19         init(null, null, name, 0);
20     }
21   //初始化线程—init()?
22     private void init(ThreadGroup g, Runnable target, String name,long stackSize) {
23         init(g, target, name, stackSize, null);
24     }
25     private void init(ThreadGroup g, Runnable target, String name,long stackSize, AccessControlContext acc) {
26         if (name == null) {
27             throw new NullPointerException("name cannot be null");
28         }
29         this.name = name.toCharArray();
30          /**后面是初始化线程代码—略*/
31     }
32 }

 

           方式二:new Thread(Runnable run)、new Thread(Runnable run,String name)

public interface Runnable {

public abstract void run();

 }


/*
*用“外置Runnable对象”覆盖、取代当前对象(目的是让注入对象也能拥有Thread对象的管理线程的方法) */ public class Thread implements Runnable { private Runnable target; public void run() { if (target != null) { target.run(); //调用“外置线程对象”的run()方法 } //除去run()的其它方法,针对的对象都是Thread.currentThread() }

 

 

3.创建自定义线程

           方式一:继承java.lang.Thread

/**
*1.构造类
*/
public
class MyThread extends Thread { //@Override //是否覆盖父类对象的run()方法 public void run() { //do something } } /**
*2.创建实例
*/
public class Main{ public void static void main(String args){ Thread t =new MyThread(); //创建Thread线程——>方式一
t.run(); //t的run()被调用
        }
}

 

           方法二:实现Runnable接口

public class MyThread implements Runnable {
    public run() {
         //do something
    }

public class Main{
    public void static void main(String args){
             MyThread mt =new MyThread();
             Thread t =new Thread(mt); //创建Thread线程——>方式二 
——————>Thread.currentThread() =>main || this对象 =>t || t的<init>被调用
t.run();
——————>Thread.currentThread() =>t || this对象 =>t || mt的run()被调用
/**因为除去
run()的其它方法,针对的对象都是Thread.currentThread(),所以*/
                   t.start();  ——————>Thread.currentThread() =>t<Thread(Runnable target)>  

|| this对象 =>t<Thread()> || t<Thread(Runnable target)>的start()被调用
                   t.stop();   ——————>Thread.currentThread() =>t<Thread(Runnable target)>  
|| this对象 =>t<Thread()> || t<Thread(Runnable target)>的stop()被调用
/**/

/*t<Thread()> 和 t<Thread(Runnable target)>的区别(即this和Thread.currentThread()的区别)?
* 答:
this指向的是new Thread()创建的那个线程实例,而不是new Thread(thread)创建的那个实例t 查看参考资料
*/

}
}

 

 

4.资源共享性

             例: class MyThread——>private int i =5;

 

            方式一:不共享


public class MyThread extends Thread {
private int i =5; public void run() { i--;
while(i>0){
System.out.println(this.currentThread().getName()+":"+i);
} } }


public
class Main{ public void static void main(String args){ Thread t1 =new MyThread("t1");
             Thread t2 =new MyThread("t2");
                    t1.start();  
                    t2.start();       
}

}

/**输出:
*t1:4
*t2:4
*t1:3
*t2: 3
* ....
*/

         

 

           方式二:共享

(实现Runnable接口方式)略

 

             原因:

   private native void start0()

关于爱程序网 - 联系我们 - 广告服务 - 友情链接 - 网站地图 - 版权声明 - 人才招聘 - 帮助