2015-10-16 15 views
1

我已經實現了生產者使用者代碼,但是我想知道的是守護程序線程是如何工作的?守護線程如何在Producer Consumer中工作?無法讓它工作

在下面的代碼生產者和消費者工作正常。 據我所知,守護線程是其他線程的服務提供者。守護線程等待其他線程死掉,所以我應該如何在程序或應用程序中使用它。

在以下代碼中,我將Producer設置爲守護進程線程,但是我沒有看到任何差異。

生產者線程死亡,不等待消費者死亡。

這是如何工作的?

package com.test; 

import com.custome.util.CustomeLinkedListImpl; 

class Produce implements Runnable { 
    private CustomeLinkedListImpl linkedList; 

    public Produce(CustomeLinkedListImpl linkedList) { 
     super(); 
     this.linkedList = linkedList; 
    } 

    @Override 
    public void run() { 
     for (int i = 1; i < 20; i++) { 
      produce(i); 
     } 

    } 

    public void produce(int i) { 

     synchronized (linkedList) { 
      while (linkedList.size() == 10) { 
       try { 
        System.out.println("Producer thread " 
          + Thread.currentThread().getName() 
          + " wait for Consumer: " + linkedList.size()); 
        linkedList.wait(); 
       } catch (InterruptedException e) { 
        e.printStackTrace(); 
       } 
      } 

     } 

     synchronized (linkedList) { 
      System.out.println("Producer thread " 
        + Thread.currentThread().getName() + " produce: " + i); 
      linkedList.addFirst(i); 
      try { 
       Thread.sleep((long) Math.random() * 1000); 
      } catch (InterruptedException e) { 
       e.printStackTrace(); 
      } 
      linkedList.notify(); 

     } 

    } 
} 

class Consume implements Runnable { 
    private CustomeLinkedListImpl linkedList; 

    public Consume(CustomeLinkedListImpl linkedList) { 
     super(); 
     this.linkedList = linkedList; 
    } 

    @Override 
    public void run() { 
     while (true) 
      consume(); 
    } 

    public void consume() { 
     synchronized (linkedList) { 
      while (linkedList.size() == 0) { 
       try { 
        System.out.println("Consumer Thread " 
          + Thread.currentThread().getName() 
          + " waiting for consumer : size=0"); 
        linkedList.wait(); 
       } catch (InterruptedException e) { 
       } 
      } 

      synchronized (linkedList) { 
       System.out.println("Consumer thread " 
         + Thread.currentThread().getName() + " consume: " 
         + linkedList.remove().getData()); 

       try { 
        Thread.sleep(500); 
       } catch (InterruptedException e) { 
        e.printStackTrace(); 
       } 
       linkedList.notify(); 
      } 
     } 
    } 

} 

public class ThreadDemo { 
    public static void main(String[] args) { 

     CustomeLinkedListImpl linkedListImpl = new CustomeLinkedListImpl(); 

     Produce producer = new Produce(linkedListImpl); 
     Consume consumer = new Consume(linkedListImpl); 

     Thread prodThread = new Thread(producer, "StackOverFlow"); 
     Thread consThread = new Thread(consumer, "Users"); 
     prodThread.setDaemon(true); 
     prodThread.start(); 
     consThread.start(); 

    } 

} 

package com.custome.util; 



public class CustomeLinkedListImpl { 

    LinkedNode first; 

    public CustomeLinkedListImpl() { 
     first = null; 
    } 

    public void addNode(int data) { 
     LinkedNode currentNode = new LinkedNode(data, null); 

     if (first == null) { 
      first = currentNode; 
      return; 
     } 

     LinkedNode tmpNode = null; 
     for (tmpNode = first; tmpNode.next != null; tmpNode = tmpNode.next) { 

     } 
     tmpNode.next = currentNode; 
    } 

    public void addFirst(int data) { 
     LinkedNode currentNode = new LinkedNode(data, null); 

     currentNode.next = first; 
     first = currentNode; 
    } 

    public void insertAtPosition(int data, int position) { 
     int i = 0; 
     LinkedNode currentNode = new LinkedNode(data, null); 
     if (position == 0) { 
      addFirst(data); 
      return; 
     } 
     LinkedNode tmpNode = first; 
     while (tmpNode.next != null) { 
      i++; 
      if (i == position) { 
       currentNode.next = tmpNode.next; 
       tmpNode.next = currentNode; 
      } else 
       tmpNode = tmpNode.next; 

     } 

    } 

    public LinkedNode remove() { 
     LinkedNode removedNode = null; 
     if (first == null) { 
      System.out.println("Null Pointer Exception"); 

     } else { 
      LinkedNode tmpNode = first; 
      first = first.next; 
      removedNode = tmpNode; 

     } 
     return removedNode; 
    } 

    public LinkedNode remove(int position) { 
     LinkedNode removedNode = null; 
     if (first == null) { 
      System.out.println("Null Pointer Exception"); 

     } else { 
      if (position == 0) { 
       first = first.next; 
      } else { 
       LinkedNode tmpNode = first; 
       int i = 0; 
       while (tmpNode != null) { 

        i++; 
        if (i == position) { 
         removedNode = tmpNode.next; 
         tmpNode.next = tmpNode.next.next; 
         break; 
        } else { 
         tmpNode = tmpNode.next; 
        } 

       } 
      } 

     } 
     return removedNode; 
    } 

    public LinkedNode removeObject(int data) { 
     LinkedNode removeNode = null; 
     LinkedNode current = first; 
     LinkedNode previous = null; 

     while (current != null) { 

      if (current.data == data) { 
       removeNode = current; 
       previous.next = current.next; 

       break; 

      } 
      previous = current; 
      current = current.next; 
     } 

     return removeNode; 

    } 

    public int size() { 
     int i = 0; 

     LinkedNode tmp = first; 
     while (tmp != null) { 
      i++; 
      tmp = tmp.next; 
     } 
     return i; 
    } 

    public void display() { 
     if (first == null) { 
      System.out.println(); 
     } else { 
      LinkedNode tmp = first; 
      while (tmp != null) { 
       System.out.println(tmp.data); 
       tmp = tmp.next; 
      } 
     } 
    } 
} 

package com.custome.util; 

public class LinkedNode { 
    int data; 
    LinkedNode next; 

    public LinkedNode(int data, LinkedNode next) { 
     super(); 
     this.data = data; 
     this.next = next; 
    } 

    public int getData() { 
     return data; 
    } 
} 
+0

後的代碼,其他的都跑,在這裏你還沒有分享您的CustomeLinkedListImpl的代碼,所以沒有人能運行你的代碼 –

回答

1

Java中的守護進程線程不阻止JVM退出。具體來說,只有守護程序線程保留時,JVM纔會退出。您可以通過調用Thread上的setDaemon()方法來創建一個。 守護線程不會等待其他線程死掉。等待其他線程與守護進程/用戶線程無關。

用戶線程和守護進程線程之間的核心區別是JVM只會在所有用戶線程終止時關閉程序。當不再有任何用戶線程運行時,守護程序線程將由JVM終止,包括主線程的執行。用戶和守護線程之間

+0

根據op的代碼發佈一些可靠的答案,因爲你的答案只是守護進程線程的定義,op不是關於這個的 –

0

差異:

  • JVM不會等待任何守護線程退出之前完成。

在這裏,你的代碼的守護線程(製片人)會活着,直到它將完成20次迭代,

問題:生產者線程死亡和不等待消費者死亡。

答:這是實際的行爲,守護線程簡化版,等待其他線程死

+0

java中的守護進程線程是向用戶線程提供服務的服務提供者線程。它的生命依賴於用戶線程的仁慈,即當所有用戶線程死亡時,JVM會自動終止該線程。 我得到了上面的句子javatpoint http://www.javatpoint.com/daemon-thread – Musaddique

+0

在多個站點和引用我讀到「守護程序線程等待用戶線程死」 – Musaddique

+0

您可以給網站提到其「守護程序線程等待用戶線程死亡「? –