多线程

多线程:

1什么是线程
线程是程序执行的一条路径,一个程序可包含多个线程
多线程可以提高程序效率,可以同时完成多个工作

2并行和并发的区别
并行就是两个任务同时运行需要多核cpu
并发是指两个任务都请求运行。而处理器只能接受一个任务,就把两个任务安排轮流进行,使人感觉两个任务都在运行。

3java程序运行原理和jvm的启动是多线程的吗
java程序运行原理
java命令会启动java虚拟机,启动jvm,等于启动了一个应用程序,也就是启动了一个进呈,它会启动一个主线程,然后去调用某个类的main方法

jvm的启动是多线程

4多线程程序实现方法


    package com.heima.thread;
    
    public class Demo2_Thread {
    	
    	public static viod main(String[] args) {
    		MyThread mt = new MyThread();
    		mt.start();
    		
    		for(int i = 0; i < 1000; i++) {
    			System.out.println("bb")
    		}
    	}
    }
    
    class MyThread extends Thread {
    	public void run() {
    		for(int i = 0; i < 1000; i++) {
    			System.out.println("aaaaaaaa");
    		}
    	}
    }

    package com.heima.thread;
    
    public class Demo2_Thread {
    	
    	public static viod main(String[] args) {
    		MyRunnable mt = new MyRunnable();
    		t.start();
    		
    		for(int i = 0; i < 1000; i++) {
    			System.out.println("bb")
    		}
    	}
    }
    
    class MyThread implements Runnable {
    	
    	@override
    	public viod run() {
    		for(int i = 0; i < 1000; i++) {
    			System.out.println("aaaaaaaa");
    		}
    	}
    }

5实现Runnable的原理

查看源码
看Thread类的结构函数,传递了Runnable接口的引用
通过init()方法找到传递的target给成员变量的target赋值
查看run的方法,发现run方法中有判断,如果target不为null就会调用Runnable接口子类对象的run方法

6两种方式的区别
继承Thread
好处是:可以直接使用类中Thread的方法。代码简单
弊端是:如果已经有了父类,就不能使用这种方法
实现Runnable接口
好处是:即使自己定义的线程类有了父类也没关系,因为有了父类也可以实现接口,而且接口可以多实现的
弊端:不能直接使用Threa中的方法需要先获取到线程对象后,才能得到Thread的方法,代码复杂

7获取名字和设置

    package com.heima.thread;
    
    public class Demo2_Thread {
    	
    	public static viod main(String[] args) {
    		new Thread() {
    			public void run() {
    				System.out.println(this.getName() + "....aaaaaaa");
    			}
    		}.start();
    		
    		new Thread() {
    			public void run() {
    				System.out.println(this.getName() + "....bbbbbb");
    			}
    		}.start
    	}
    }
    package com.heima.thread;
    
    public class Demo2_name {
    	
    	public static viod main(String[] args) {
    		Thread t1 = new Thread() {
    			public void run() {
    				//this.seName("张三");
    				System.out.println(this.getName() + "....aaaaaaa");
    			}
    		}

    		Thread t1 = new Thread() {
    			public void run() {
    				//this.seName("李四");
    				System.out.println(this.getName() + "....bbbbbb");
    			}
    		};
   }		
    		t1.setName("张三");
    		t2.setName("李四");
    		t1.start()}
    }

8 获取当前线程对象

    package com.heima.threadmethod;
    
    public class Demo2_CurrentThread {
    	
    	public static viod main(String[] args) {
    		new Thread() {
    			public void run() {
    				System.out.println(this.getName() + "....aaaaa");
    			}
    		}start();
    		
    		new Thread(new Runnable() {
    			public void run() {
    				System.out.println(Thread.currentThread() + "....bbbbbb");
    			}
    		}.start();
    		
    		Thread.currentThread().setName("我是主线程")
    		System.out.println(Thread.currentThread().getName());
    	}
    	
    }

9休眠线程

    package com.heima.threadmethod;
    
    public class Demo2_Sleep {
    	
    	public static viod main(String[] args)throws InterruptedException {						
    		new Thread() {
    			public void run() {
    				for(int i = 20; i >=0; i--) {
    					try {
    						Thread.sleep(1000);
    					}catch (InterruptedException e) {
    				
    						e.printStackTrace();
    				
    					}
    					System.out.println(Thread.currentThread() + "....aaaaa");
    			
    				}
    			}
    		}.start();
    		
    		new Thread() {
    			public void run() {
    				for(int i = 20; i >=0; i--) {
    					try {
    						Thread.sleep(1000);
    					}catch (InterruptedException e) {
    				
    						e.printStackTrace();
    				
    					}
    					System.out.println(Thread.currentThread() + "....bbbbbb");
    			
    				}
    			}
    		}.start();
    	}
    	
    }

10守护线程

    package com.heima.threadmethod;
    
    public class Demo2_Daemon {
    	
    	public static viod main(String[] args) {
    		Thread t1 = new Thread() {
    			public void run() {
    				for(int i = 0; i < 2; i++) {
    					System.out.println(Thread.currentThread() + "....aaaaa");
    				}
    			}
    		}
    		Thread t2 = new Thread() {
    			public void run() {
    				for(int i = 0; i < 50; i++) {
    					System.out.println(Thread.currentThread() + "....bb");
    				}
    				
    			}
    			
    		}
    		t2.setDaemon(true);
    		t1.start();
    		t2.start();
    		
    	}
    	
    }

11加入线程

    package com.heima.threadmethod;
    
    public class Demo2_Join {
    	
    	public static viod main(String[] args) {
    		Thread t1 = new Thread() {
    			public void run() {
    				for(int i = 0; i < 10; i++) {
    					System.out.println(getName() + "....aaaaa");
    				}
    			}
    		};
    		Thread t2 = new Thread() {
    			public void run() {
    				for(int i = 0; i < 10; i++) {
    					if(i == 2) {
    						//t1.join();
    						t1.join(1);
    					} catch (InterruptedException e) {
    						e.printStackTrace();
    					}
    				}
    				System.out.println(TgetName() + "....bb");
    				}
    			}
    		};
    		
    		t1.start();
    		t2.start();
    	}
    }

12礼让线程

    package com.heima.threadmethod;
    
    public class Demo2_Join {
    	
    	public static viod main(String[] args) {
    		new MyThread().start();
    		new MyThread().start();
    	}
    	
    }
    class MyThread extends Thread {
    	public void run() {
    		for(int i = 1; i <= 1000; i++) {
    			if(i % 10 == 0) {
    				Thread.yield();   //让出cpu
    				System.out.println(getName() + "..." + i)
    			}
    		}
    	}
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值