我已經實現了生產者使用者代碼,但是我想知道的是守護程序線程是如何工作的?守護線程如何在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;
}
}
後的代碼,其他的都跑,在這裏你還沒有分享您的CustomeLinkedListImpl的代碼,所以沒有人能運行你的代碼 –