• thread 多线程2


    ###24.04_多线程(多线程程序实现的方式1)(掌握)
    * 1.继承Thread
    	* 定义类继承Thread
    	* 重写run方法
    	* 把新线程要做的事写在run方法中
    	* 创建线程对象
    	* 开启新线程, 内部会自动执行run方法
    	* 
    		
    			public class Demo2_Thread {
    		
    				/**
    				 * @param args
    				 */
    				public static void main(String[] args) {
    					MyThread mt = new MyThread();							//4,创建自定义类的对象
    					mt.start();												//5,开启线程
    					
    					for(int i = 0; i < 3000; i++) {
    						System.out.println("bb");
    					}
    				}
    			
    			}
    			class MyThread extends Thread {									//1,定义类继承Thread
    				public void run() {											//2,重写run方法
    					for(int i = 0; i < 3000; i++) {							//3,将要执行的代码,写在run方法中
    						System.out.println("aaaaaaaaaaaaaaaaaaaaaaaaaaaa");
    					}
    				}
    			}
    
    ###24.05_多线程(多线程程序实现的方式2)(掌握)
    * 2.实现Runnable
    	* 定义类实现Runnable接口
    	* 实现run方法
    	* 把新线程要做的事写在run方法中
    	* 创建自定义的Runnable的子类对象
    	* 创建Thread对象, 传入Runnable
    	* 调用start()开启新线程, 内部会自动调用Runnable的run()方法
    
    			public class Demo3_Runnable {
    				/**
    				 * @param args
    				 */
    				public static void main(String[] args) {
    					MyRunnable mr = new MyRunnable();						//4,创建自定义类对象
    					//Runnable target = new MyRunnable();
    					Thread t = new Thread(mr);								//5,将其当作参数传递给Thread的构造函数
    					t.start();												//6,开启线程
    					
    					for(int i = 0; i < 3000; i++) {
    						System.out.println("bb");
    					}
    				}
    			}
    			
    			class MyRunnable implements Runnable {							//1,自定义类实现Runnable接口
    				@Override
    				public void run() {											//2,重写run方法
    					for(int i = 0; i < 3000; i++) {							//3,将要执行的代码,写在run方法中
    						System.out.println("aaaaaaaaaaaaaaaaaaaaaaaaaaaa");
    					}
    				}
    				
    			}
    
    ###24.06_多线程(实现Runnable的原理)(了解)
    * 查看源码
    	* 1,看Thread类的构造函数,传递了Runnable接口的引用 
    	* 2,通过init()方法找到传递的target给成员变量的target赋值
    	* 3,查看run方法,发现run方法中有判断,如果target不为null就会调用Runnable接口子类对象的run方法
    
    ###24.07_多线程(两种方式的区别)(掌握)
    * 查看源码的区别:
    	* a.继承Thread : 由于子类重写了Thread类的run(), 当调用start()时, 直接找子类的run()方法
    	* b.实现Runnable : 构造函数中传入了Runnable的引用, 成员变量记住了它, start()调用run()方法时内部判断成员变量Runnable的引用是否为空, 不为空编译时看的是Runnable的run(),运行时执行的是子类的run()方法
    	
    * 继承Thread
    	* 好处是:可以直接使用Thread类中的方法,代码简单
    	* 弊端是:如果已经有了父类,就不能用这种方法
    * 实现Runnable接口
    	* 好处是:即使自己定义的线程类有了父类也没关系,因为有了父类也可以实现接口,而且接口是可以多实现的
    	* 弊端是:不能直接使用Thread中的方法需要先获取到线程对象后,才能得到Thread的方法,代码复杂
    ###24.08_多线程(匿名内部类实现线程的两种方式)(掌握)
    * 继承Thread类
    	 	
    		new Thread() {													//1,new 类(){}继承这个类
    			public void run() {											//2,重写run方法
    				for(int i = 0; i < 3000; i++) {							//3,将要执行的代码,写在run方法中
    					System.out.println("aaaaaaaaaaaaaaaaaaaaaaaaaaaa");
    				}
    			}
    		}.start();
    * 实现Runnable接口
    			
    		new Thread(new Runnable(){										//1,new 接口(){}实现这个接口
    			public void run() {											//2,重写run方法
    				for(int i = 0; i < 3000; i++) {							//3,将要执行的代码,写在run方法中
    					System.out.println("bb");
    				}
    			}
    		}).start(); 



    ###24.16_多线程(同步代码块)(掌握)
    * 1.什么情况下需要同步
    	* 当多线程并发, 有多段代码同时执行时, 我们希望某一段代码执行的过程中CPU不要切换到其他线程工作. 这时就需要同步.
    	* 如果两段代码是同步的, 那么同一时间只能执行一段, 在一段代码没执行结束之前, 不会执行另外一段代码.
    * 2.同步代码块
    	* 使用synchronized关键字加上一个锁对象来定义一段代码, 这就叫同步代码块
    	* 多个同步代码块如果使用相同的锁对象, 那么他们就是同步的
    
    			class Printer {
    				Demo d = new Demo();
    				public static void print1() {
    					synchronized(d){				//锁对象可以是任意对象,但是被锁的代码需要保证是同一把锁,不能用匿名对象
    						System.out.print("黑");
    						System.out.print("马");
    						System.out.print("程");
    						System.out.print("序");
    						System.out.print("员");
    						System.out.print("
    ");
    					}
    				}
    	
    				public static void print2() {	
    					synchronized(d){	
    						System.out.print("传");
    						System.out.print("智");
    						System.out.print("播");
    						System.out.print("客");
    						System.out.print("
    ");
    					}
    				}
    			}
    ###24.17_多线程(同步方法)(掌握)
    * 使用synchronized关键字修饰一个方法, 该方法中所有的代码都是同步的
    
    		class Printer {
    			public static void print1() {
    				synchronized(Printer.class){				//锁对象可以是任意对象,但是被锁的代码需要保证是同一把锁,不能用匿名对象
    					System.out.print("黑");
    					System.out.print("马");
    					System.out.print("程");
    					System.out.print("序");
    					System.out.print("员");
    					System.out.print("
    ");
    				}
    			}
    			/*
    			 * 非静态同步函数的锁是:this
    			 * 静态的同步函数的锁是:字节码对象
    			 */
    			public static synchronized void print2() {	
    				System.out.print("传");
    				System.out.print("智");
    				System.out.print("播");
    				System.out.print("客");
    				System.out.print("
    ");
    			}
    		}


  • 相关阅读:
    Pset_DistributionChamberElementTypeFormedDuct
    Pset_DistributionChamberElementTypeInspectionChamber
    Pset_DistributionChamberElementTypeInspectionPit
    错误日志
    Pset_DistributionChamberElementTypeManhole
    Pset_DistributionChamberElementTypeMeterChamber
    osg绘制立方体
    Pset_AlarmPHistory
    Pset_ActuatorTypeRotationalActuation
    Pset_ActuatorTypePneumaticActuator
  • 原文地址:https://www.cnblogs.com/gaoyang666/p/11651184.html
Copyright © 2020-2023  润新知