Skip to content

Commit

Permalink
producer and consumer
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangyuwei committed Oct 8, 2019
1 parent 23d98b0 commit bd03746
Show file tree
Hide file tree
Showing 11 changed files with 360 additions and 0 deletions.
9 changes: 9 additions & 0 deletions .idea/JavaLearn.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/sbt.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

124 changes: 124 additions & 0 deletions .idea/uiDesigner.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions AboutClass/AboutClass.iml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="jdk" jdkName="1.8" jdkType="JavaSDK" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
18 changes: 18 additions & 0 deletions AboutClass/src/InnerClass.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@

class InnerClass {
static class Test{
int num = 0;
Test(int n){
num = n;
}
}
}

class Main{
public static void main(String[] args){
InnerClass.Test test1 = new InnerClass.Test(1);
InnerClass.Test test2 = new InnerClass.Test(2);
System.out.println(test1.num);
System.out.println(test2.num);
}
}
11 changes: 11 additions & 0 deletions Producer&Consumer/Producer&Consumer.iml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="jdk" jdkName="1.8" jdkType="JavaSDK" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
77 changes: 77 additions & 0 deletions Producer&Consumer/src/BlockingQueue.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import java.util.concurrent.BlockingDeque;
import java.util.concurrent.LinkedBlockingDeque;

class QueueManager{
private LinkedBlockingDeque<Integer> queue;
QueueManager(){
this.queue = new LinkedBlockingDeque(50);
}

void put(Integer ele){
try {
queue.put(ele);
}catch(InterruptedException e) {
Thread.currentThread().interrupt();
}
}

Integer take(){
try{
return queue.take();
}catch (InterruptedException e){
Thread.currentThread().interrupt();
}
return null;
}
}

class Producer extends Thread{
private QueueManager queue;
Producer(QueueManager q){
this.queue = q;
}

public void run(){
for(int i=0;i<100;i++){
System.out.println("put " + i);
// try{
// Thread.sleep(1000);
// }catch (InterruptedException e){
// Thread.currentThread().interrupt();
// }
queue.put(i);
}
}
}

class Consumer extends Thread{
private QueueManager queue;

Consumer(QueueManager q){
this.queue = q;
}

public void run(){
for(int i=0;i<100;i++){
int a = queue.take();
try{
Thread.sleep(1000);
}catch (InterruptedException e){
Thread.currentThread().interrupt();
}
System.out.println(a);
}
}
}

class Test{
public static void main(String[] args){
QueueManager queue = new QueueManager();
Producer producer = new Producer(queue);
Consumer consumer = new Consumer(queue);
producer.start();
consumer.start();
}
}


79 changes: 79 additions & 0 deletions Producer&Consumer/src/LockTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import java.util.LinkedList;

public class LockTest {
private int size = 50;
private LinkedList<Integer> list = new LinkedList<>();

synchronized public void put(int num){
if (isFull()){
try {
wait(); //进入等待状态可以自己激活么
System.out.println("=============");
}catch (InterruptedException e){
Thread.currentThread().interrupt();
}
}
System.out.println("put" + num);
list.add(num);
notifyAll();
}


public boolean isEmpety(){
return list.isEmpty();
}

public boolean isFull(){
return list.size() == size;
}

synchronized public void take(){
while (isEmpety()){
try {
wait();
}catch (InterruptedException e){
Thread.currentThread().interrupt();
}
}
System.out.println(list.getFirst());
list.remove();
notifyAll();
}

public static void main(String[] args){
LockTest lockTest = new LockTest();
Producer1 producer1 = new Producer1(lockTest);
Consumer1 consumer1 = new Consumer1(lockTest);
consumer1.start();
producer1.start();

}
}

class Producer1 extends Thread{
private LockTest lockTest;
Producer1(LockTest lockTest){
this.lockTest = lockTest;
}

public void run(){
for(int i=0;i<100;i++){
lockTest.put(i);
}
}
}


class Consumer1 extends Thread{
private LockTest lockTest;
Consumer1(LockTest lockTest){
this.lockTest = lockTest;
}

public void run(){
for(int i=0;i<100;i++){
lockTest.take();
}
}
}

0 comments on commit bd03746

Please sign in to comment.