zoukankan      html  css  js  c++  java
  • 多线程第一节------------创建线程的两种方法

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

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

  • 相关阅读:
    form表单提交后保持输入的值
    django 自定义属性
    linux修改mysql密码
    linux下备份mysql数据库
    《小学数学辅导》服务协议
    《小升初辅导》服务协议
    《小学数学试题练习》服务协议
    清明节应该回高中学校扫扫墓,因为那里埋葬了你的青春。
    [转]智能聊天机器人小黄鸡及其制作方法
    人们问我,长大了要做什么?我写下“快乐”,他们说我理解错了题目。我告诉他们,他们理解错了人生。—— 约翰·列侬
  • 原文地址:https://www.cnblogs.com/chengpeng15/p/5969368.html
Copyright © 2011-2022 走看看