线程中涉及到的知识点总结

一、线程中涉及到知识的总结

1、进程:

   就是正在运行的程序,分配内存让应用程序能够运行。

2、线程:

   一个进程中负责代码的执行,就是一个进程中执行的路径。

3、多线程

      就是在一个进程中多个执行路径同时执行。

4、线程中常用的方法。

 Thread(String name)----------------------初始化线程的名字

 getName() ----------------------------------返回线程的名字

 setName(String name)-------------------设置线程对象名

 sleep() ---------------------------------------线程睡眠指定的毫秒数。

 getPriority()  -----------------------------返回当前线程对象的优先级,默认线程的优先级是5

 setPriority(int newPriority) 设置线程的优先级,虽然设置了线程的优先级,但是具体的实现取决于底层的操作系统的实现(最大的优先级是10 ,最小的1 , 默认是5

currentThread()-------------------------返回CPU正在执行的线程的对象

二、创建线程有两种方式

       一、Thread (线程类):

 1 package com.lw.thread;
 2 class SaleTickets extends Thread{
 3     
 4     static int num = 100;// 总票数 (共享的数据) ,三个窗口同时操作一份数据
 5     
 6     static Object o = new Object();
 7     
 8     public SaleTickets(String name){
 9         
10         super(name);
11     }
12     
13     public static synchronized void print(){ //锁对象是SaleTickets的字节码文件
14         
15         System.out.println("这是一个同步的静态方法");
16     }
17     
18     
19     //重写run方法 : 卖票的任务
20     @Override
21     public synchronized void run() {  // 谁调用,锁对象就是谁
22         //用一个循环来模拟卖票功能
23         while(true){
24             
25             synchronized ("锁") { // 任意类型的对象  ,锁对象应该是同一个对象  
26                 
27                 try {
28                     Thread.sleep(500);  //窗口1进来了   窗口1停售0.5秒  
29                 } catch (InterruptedException e) {
30                     // TODO Auto-generated catch block
31                     e.printStackTrace();
32                 }
33                 
34                 if(num > 0){ // num大于零代表有票 
35                     
36                     System.out.println(this.getName()+"卖了第"+num+"张票");
37                     num--;
38                 }else {
39                     
40                     System.out.println("票以售完");
41                     break;
42                 }
43             }
44             
45         }
46         
47         
48     }
49 }
50 
51 
52 public class Demo4 {
53 
54     /**
55      * @param args
56      */
57     public static void main(String[] args) {
58         
59         //创建三个窗口
60         SaleTickets t1 = new SaleTickets("窗口1");
61         SaleTickets t2 = new SaleTickets("窗口2");
62         SaleTickets t3 = new SaleTickets("窗口3");
63         //打开窗口卖票
64         t1.start();
65         t2.start();
66         t3.start();
67 
68     }
69 
70 }

二、Runable (线程类):

 1 package com.lw.thread;
 2 
 3 /*
 4  * 以Runable实现窗口卖票功能
 5  */
 6 
 7 class SaleTickts implements Runnable{
 8     
 9     private int tickts = 50;
10     
11     @Override
12     public void run() {
13         // TODO Auto-generated method stub
14         
15         while(true){
16             synchronized ("锁") {
17                 if(tickts > 0){
18                     System.out.println(Thread.currentThread().getName()+"卖出第"+tickts+"张票");
19                     tickts--;
20                 }else {
21                     
22                     System.out.println("票以售完");
23                     break;
24                 }
25             }
26         }
27     }
28     
29 }
30 
31 public class Demo2{
32 
33     /**
34      * @param args
35      */
36     public static void main(String[] args) {
37         // TODO Auto-generated method stub
38         //创建实现类对象
39         SaleTickts s = new SaleTickts(); //对象只需要创建一个
40         //创建三个线程对象
41         Thread t1 = new Thread(s, "窗口1");
42         Thread t2 = new Thread(s, "窗口2");
43         Thread t3 = new Thread(s, "窗口3");
44         t1.start();
45         t2.start();
46         t3.start();
47         
48     }
49 
50 }

三、线程加锁

synchronized (锁对象) {

   } 

四、线程的通讯:

  1、理解一个线程完成自己的任务,去通知另外一个线程去完成另外一个任务。

  wait();--------等待-------如果线程执行了wait方法 ,那么该线程就会处于一个等待状态,等待状态的线程必须要通过其他线程来调用。

  notify()---------------------------------------------------------方法来唤醒。

  notify();---------------唤醒---------随机唤醒线程池中的一个线程。

  notifyAll();--------------------------------------- 唤醒所有等待的线程。

2、waitnotify的使用注意点 :

    1.wait方法和notify方法是属性Object对象。

    2.wait方法和notify方法必须在同步线程中执行。

    3.wait方法和notify方法必须有锁对象来调用。

3、示例代码:

  1 package com.lw.thread;
  2 class Product {
  3     
  4     String name;
  5     double price;
  6     
  7 }
  8 
  9 class Producter extends Thread{  //生产产品
 10     Product p;
 11     
 12     public Producter(Product p){
 13         
 14         this.p = p;
 15     }
 16     @Override
 17     public void run() {
 18         // TODO Auto-generated method stub
 19         
 20         for(int i = 0;i<50;i++){
 21             
 22             synchronized (p) {
 23                 
 24                 if(i % 2 == 0){
 25                     p.name = "苹果";
 26                     p.price = 3.5;
 27                     System.out.println(p.name+":"+p.price);
 28                 }else {
 29                     
 30                     p.name = "香蕉";
 31                     p.price = 2.0;
 32                     System.out.println(p.name+":"+p.price);
 33                 }
 34                 
 35                 //产品以经生产出来 唤醒消费者来消费
 36                 p.notify();
 37                 
 38                 try {
 39                     p.wait(); // 等待消费者提醒
 40                 } catch (InterruptedException e) {
 41                     // TODO Auto-generated catch block
 42                     e.printStackTrace();
 43                 }    
 44             }
 45         }
 46     }
 47 }
 48 
 49 class Custorm extends Thread{
 50     
 51     Product p; // 消费者消费的产品
 52     
 53     public Custorm(Product p){
 54         
 55         this.p = p;
 56     }
 57     
 58     @Override
 59     public void run() {
 60         // TODO Auto-generated method stub
 61         
 62         for (int i = 0;i<50;i++){
 63             synchronized (p) {
 64                 //通过锁对象来调用
 65                 if(p.name != null){
 66                     System.out.println("吃"+p.name+":"+p.price);
 67                 } // 没有产品  告诉生产者需要生产产品
 68                     
 69                 //唤醒生产者
 70                 p.notify();
 71                 try {
 72                     p.wait();  //等待产品创建
 73                 } catch (InterruptedException e) {
 74                     // TODO Auto-generated catch block
 75                     e.printStackTrace();
 76                 }
 77             }
 78         }
 79     }
 80 }
 81 
 82 public class Demo3 {
 83 
 84     /**
 85      * @param args
 86      */
 87     public static void main(String[] args) {
 88         // TODO Auto-generated method stub
 89         //创建一个产品对象
 90         Product p = new Product();
 91         
 92         //创建一个生产者
 93         Producter t1 = new Producter(p);
 94         
 95         //创建一个消费者
 96         Custorm t2 = new Custorm(p);
 97         
 98         t1.start();
 99         t2.start();
100         
101     }
102 }
原文地址:https://www.cnblogs.com/lwlxqlccc/p/6127774.html