码迷,mamicode.com
首页 > 编程语言 > 详细

黑马程序员——多线程总结

时间:2015-12-26 14:50:08      阅读:272      评论:0      收藏:0      [点我收藏+]

标签:

                     ------Java培训、Android培训、iOS培训、.Net培训、期待与您交流! -------
多线程和进程的区别?
简而言之,一个程序至少有一个进程,一个进程至少有一个线程.
线程的划分尺度小于进程,使得多线程程序的并发性高。
另外,进程在执行过程中拥有独立的内存单元,而多个线程共享内存,从而极大地提高了程序的运行效率。
线程在执行过程中与进程还是有区别的。每个独立的线程有一个程序运行的入口、顺序执行序列和程序的出口。但是线程不能够独立执行,必须依存在应用程序中,由应用程序提供多个线程执行控制。
从逻辑角度来看,多线程的意义在于一个应用程序中,有多个执行部分可以同时执行。但操作系统并没有将多个线程看做多个独立的应用,来实现进程的调度和管理以及资源分配。这就是进程和线程的重要区别。 
 
创建线程的第一种方式
子类继承Thread类,并覆盖Thread类中的run()方法
 
创建线程的第二种方式
类实现Runnable接口中的run()方法
 
同步代码块
synchronized(锁){
同步代码区,
}
同步函数
public synchronized void show(){
同步代码区,
}
 1 练习
 2 package blogtest3;
 3 
 4 public class ThreadTest1 {
 5 
 6  /**
 7   * 创建两个线程,和主线程交替运行
 8   */
 9  public static void main(String[] args) {
10   Thread1 thread1 = new Thread1("one");
11   Thread2 thread2  = new Thread2("two");
12   thread1.start();
13   thread2.start();
14   int z =0;
15   while(true){
16    System.out.println(Thread.currentThread().getName()+z++);
17    if(z >= 100)
18     break;
19   }
20  }
21 
22 }
23 class Thread1 extends Thread{
24  private int i = 0;
25  Thread1(String name){
26   super(name);
27  }
28  public void run(){
29   while(true){
30    System.out.println(Thread.currentThread().getName()+i++);
31    if(i>100)
32     break;
33   }
34  }
35 }
36 class Thread2 extends Thread{
37  private int j = 0;
38  Thread2(String name){
39   super(name);
40  }
41  public void run(){
42   while(true){
43    System.out.println(this.getName()+j++);
44    if(j>100)
45     break;
46   }
47  }
48 }
 1 练习
 2 package blogtest3;
 3 
 4 public class ThreadTest2 {
 5 
 6  /**
 7   * 编写一个卖票程序
 8   */
 9  public static void main(String[] args) {
10   Ticket ticket = new Ticket();
11   Thread thread1 = new Thread(ticket);
12   Thread thread2 = new Thread(ticket);
13   Thread thread3 = new Thread(ticket);
14   thread1.start();
15   thread2.start();
16   thread3.start();
17  
18  }
19 
20 }
21 class Ticket implements Runnable{
22  private int num = 1500;
23  public void run(){
24   synchronized(this){
25    while(true){
26     if(num > 0)
27      System.out.println(Thread.currentThread().getName() + "   " + num--);
28    }
29    
30    }
31   }
32  
33 }
 1 练习
 2 package blogtest3;
 3 
 4 public class ThreadTest3 {
 5 
 6  /**
 7   * 单例设计模式
 8   */
 9  public static void main(String[] args) {
10   Student stu  = Student.getStudent();
11   People peo = People.getPeople();
12   System.out.println(stu);
13   System.out.println(peo);
14 
15  }
16 
17 }
18 //懒汉式
19 class Student{
20  private static   Student student = null;
21  private Student(){
22  
23  }
24  public synchronized static Student getStudent(){
25   if(student == null){
26    student = new Student();
27   }
28   return student;
29  }
30 }
31 //饿汉式
32 class People{
33  private static final People people = new People();
34  private People(){
35  
36  }
37  public static People getPeople(){
38   return people;
39  }
40 }
 1 练习
 2 package blogtest3;
 3 
 4 public class ThreadTest4 {
 5 
 6  /**
 7   * 线程间通信
 8   */
 9  public static void main(String[] args) {
10   Res res = new Res();
11   Input input = new Input(res);
12   Output output = new Output(res);
13   Thread thread1 = new Thread(input);
14   Thread thread2 = new Thread(output);
15   thread1.start();
16   thread2.start();
17 
18  }
19 
20 }
21 class Res{
22  String name;
23  String sex;
24  boolean blag = false;
25 }
26 class Input implements Runnable{
27  private Res res;
28  Input(Res res){
29   this.res = res;
30  }
31  public void run(){
32   boolean flag = false;
33  
34   while(true){
35    synchronized(res){
36     if(res.blag){
37      try {
38       res.wait();
39      } catch (InterruptedException e) {
40       e.printStackTrace();
41      }
42     }
43     if( !flag ){
44      res.name = "zhangshan";
45      res.sex = "nan";
46      flag = true;
47     }
48     else{
49      res.name = "Lili";
50      res.sex = "nv";
51      flag = false;
52     }
53     res.blag = true;
54     res.notify();
55    }
56   }
57  }
58 }
59 
60 class Output implements Runnable{
61  private Res res;
62  Output(Res res){
63   this.res = res;
64  }
65  public void run(){
66   while(true){
67    synchronized(res){
68     if(!res.blag){
69      try {
70       res.wait();
71      } catch (InterruptedException e) {
72       e.printStackTrace();
73      }
74     }
75      System.out.println("name = " + res.name + "sex = " + res.sex);
76      res.blag = false;
77      res.notify();
78     }
79    
80    }
81    
82   }
83  }
 1 练习
 2 package blogtest3;
 3 
 4 public class ThreadTest5 {
 5 
 6  /**
 7   * 用多线程实现多个生产者和多个消费者的活动
 8   */
 9  public static void main(String[] args) {
10   Factory factory = new Factory();
11   new Thread(new Action1(factory)).start();
12   new Thread(new Action1(factory)).start();
13   new Thread(new Action1(factory)).start();
14   new Thread(new Action2(factory)).start();
15   new Thread(new Action2(factory)).start();
16   new Thread(new Action2(factory)).start();
17  }
18 
19 }
20 class Factory{
21  private int milk = 0;
22  boolean flag = false;
23  public void product(){
24   System.out.println("productor = " +(++milk));
25  }
26  public void consumer(){
27   System.out.println("consumer = " + milk);
28  }
29 }
30 class Action1 implements Runnable {
31  private Factory factory;
32  Action1(Factory factory){
33   this.factory = factory;
34  }
35  public void run(){
36   while(true){
37    synchronized(factory){
38     while(factory.flag){
39      try {
40       factory.wait();
41      } catch (InterruptedException e) {
42       e.printStackTrace();
43      }
44     }
45     factory.product();
46     factory.flag = true;
47     factory.notifyAll();
48    }
49   }
50  }
51  
52 }
53 
54 class Action2 implements Runnable{
55  private Factory factory;
56  Action2(Factory factory){
57   this.factory = factory;
58  }
59  public void run(){
60   while(true){
61    synchronized(factory){
62     while(!factory.flag){
63      try {
64       factory.wait();
65      } catch (InterruptedException e) {
66       e.printStackTrace();
67      }
68     }
69     factory.consumer();
70     factory.flag = false;
71     factory.notifyAll();
72    }
73   }
74  }
75 }

 

黑马程序员——多线程总结

标签:

原文地址:http://www.cnblogs.com/yuemingxingxing/p/5077998.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!