多线程第一节------------创建线程的两种方法

package ThreadTest;

public class TraditionalThread01 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
//		Thread thread = new Thread(){};//这种写法是Thread的子类
		Thread thread = new Thread(){
			@Override
			public void run() {
				while(true){
					try {
						Thread.sleep(500);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					System.out.println("1"+Thread.currentThread().getName());//Thread.currentThread()表示当前线程
					System.out.println("2"+this.getName());//this 代表run方法所在的对象

				}
			}
			
		};
		thread.start();
//		System.out.println(Thread.currentThread().getName());//main
	}

}

  

注意下this表示执行run方法的对象,并不一定是线程,也可能是runnable对象

package ThreadTest;

public class TraditionalThread2 {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        // Thread thread1 = new Thread(){};//这种写法是Thread的子类
        Thread thread1 = new Thread() {// 这种方式覆盖父类的run方法
            @Override
            public void run() {
                while (true) {
                    try {
                        Thread.sleep(500);
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    System.out.println("1" + Thread.currentThread().getName());// Thread.currentThread()表示当前线程
//                    System.out.println("2" + this.getName());// this 代表run方法所在的对象
                                                                

                }
            }

        };
        thread1.start();

        Thread thread2 = new Thread(new Runnable() {

            @Override
            public void run() {
                try {
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                System.out.println("11" + Thread.currentThread().getName());// Thread.currentThread()表示当前线程

                // this 代表run方法所在的对象,是runnable对象,他不是线程没有getName方
                // System.out.println("222"+this.getName());

            }
        });

        thread2.start();
    }

}

 

package ThreadTest;

public class TraditionalThread3 {

    public static void main(String[] args) {
        
        //匿名类创建线程
        new Thread(new Runnable() {
            public void run() {
                while (true) {
                    try {
                        Thread.sleep(500);
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    System.out.println("1" + Thread.currentThread().getName());// Thread.currentThread()表示当前线程
//                    System.out.println("2" + this.getName());// this 代表run方法所在的对象
                                                                

                }
            };
        }).start();
        
        
        
        new Thread(){
            public void run() {
                while (true) {
                    try {
                        Thread.sleep(500);
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    System.out.println("2" + Thread.currentThread().getName());// Thread.currentThread()表示当前线程
                }
            };
        }.start();
    }

//     TraditionalThread4 会把这个两个混在一起
    

}

 

package ThreadTest;
/*
 * 下面这个例子最终运行的是thread的run方法
 * new Thread(){}.start是子类
 * new Thread(new Runnable()....).start还是该方法
 * new Thread(runnalbe.run){run}   子类有run方法,就不用找父类runnable的
 */
public class TraditionalThread4 {

	public static void main(String[] args) {

		// 匿名类创建线程
		new Thread(new Runnable() {
			public void run() {
				while (true) {
					try {
						Thread.sleep(500);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					System.out.println("runnable:" + Thread.currentThread().getName());// Thread.currentThread()表示当前线程

				}
			};
		}){
			public void run() {
				while (true) {
					try {
						Thread.sleep(500);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					System.out.println("thread:" + Thread.currentThread().getName());// Thread.currentThread()表示当前线程
				}
			};
		}.start();
	}

}

  关于多线程的理解-------------多线程是抢占别人的资源  而不是让自己变快---------------以后再看这个理解对不对

posted @ 2016-10-17 13:01  鹏鹏进阶  阅读(198)  评论(0编辑  收藏  举报