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 18, 2019
1 parent bd03746 commit 3ce1aea
Show file tree
Hide file tree
Showing 8 changed files with 147 additions and 5 deletions.
2 changes: 2 additions & 0 deletions .idea/modules.xml

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

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

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

synchronized public void put(int num){
if (isFull()){
try {
System.out.println("0000000000000000");
wait(); //进入等待状态可以自己激活么
System.out.println("=============");
}catch (InterruptedException e){
Expand All @@ -15,6 +16,7 @@ synchronized public void put(int num){
}
System.out.println("put" + num);
list.add(num);
System.out.println("size" + list.size());
notifyAll();
}

Expand All @@ -37,15 +39,21 @@ synchronized public void take(){
}
System.out.println(list.getFirst());
list.remove();
notifyAll();
notify();
}

public static void main(String[] args){
LockTest lockTest = new LockTest();
Producer1 producer1 = new Producer1(lockTest);
Consumer1 consumer1 = new Consumer1(lockTest);
consumer1.start();
// consumer1.start();
producer1.start();
try {
Thread.sleep(1000);
}catch (InterruptedException e){

}
consumer1.start();

}
}
Expand All @@ -57,7 +65,7 @@ class Producer1 extends Thread{
}

public void run(){
for(int i=0;i<100;i++){
for(int i=0;i<5;i++){
lockTest.put(i);
}
}
Expand All @@ -71,7 +79,7 @@ class Consumer1 extends Thread{
}

public void run(){
for(int i=0;i<100;i++){
for(int i=0;i<1;i++){
lockTest.take();
}
}
Expand Down
11 changes: 11 additions & 0 deletions algorithm/algorithm.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="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
39 changes: 39 additions & 0 deletions algorithm/src/InsertMerge.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import java.util.Arrays;

public class InsertMerge {
public static int[][] insert(int[][] intervals, int[] newInterval) {
int[][] result = new int[intervals.length + 1][2];
int len = 0;
for(int i=0; i< intervals.length; i++){
if(newInterval[0] > intervals[i][0]) {
System.arraycopy(intervals, 0, result, 0, i);
if (intervals[i][1] >= newInterval[0]) {
int head = Math.min(intervals[i][0], newInterval[0]);
int tail = newInterval[1];
for (int j = i + 1; j < intervals.length; j++) {
if (intervals[j][0] <= tail) tail = Math.max(tail, intervals[0][1]);
else {
result[i] = new int[]{head, tail};
System.arraycopy(intervals, j, result, i + 1, intervals.length - j);
return Arrays.copyOf(result, i + 1 + intervals.length - j);
}
}
result[i] = new int[]{head, tail};
return Arrays.copyOf(result, i + 1);
}
}else{

}
}
System.arraycopy(intervals,0,result,0,intervals.length);
result[intervals.length] = newInterval;
return result;
}

public static void main(String[] args){
int[][] intervals =new int[][]{{1,3},{6,9}};
int[] newInterval =new int[]{4,5};
int[][] result = insert(intervals, newInterval);
System.out.println(Arrays.toString(result[1]));
}
}
37 changes: 37 additions & 0 deletions algorithm/src/JumpGame.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
//给定一个非负整数数组,你最初位于数组的第一个位置。
// 数组中的每个元素代表你在该位置可以跳跃的最大长度。
// 判断你是否能够到达最后一个位置。
// 示例 1:
// 输入: [2,3,1,1,4]
// 输出: true
// 解释: 从位置 0 到 1 跳 1 步, 然后跳 3 步到达最后一个位置。



//自顶向下的动态规划,采用递归算法 是回溯算法的优化
//自下向上的动态规划 是先从已知条件入手 然后一步一步使已知条件靠近结果
class JumpGame
{
public boolean canJump(int[] nums) {
int len = nums.length - 1;
int i = 0;
while(i<=len){
if(i + nums[i] >= len){
return true;
}else if(nums[i] == 0){
return false; //要考虑到此处3为0, 永远也走不出去的情况
}
int maxStep = 0;
int maxI = 0;
for(int j=i+1; j<=Math.min(i+nums[i],len);j++){
int tmpStep = j-i+nums[j];
if(tmpStep >= maxStep ){
maxStep = tmpStep;
maxI = j;
}
}
i = maxI;
}
return false;
}
}
31 changes: 31 additions & 0 deletions algorithm/src/Merge.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import java.lang.reflect.Array;
import java.util.*;

public class Merge {

public class MyComparable implements Comparator<int[]> {
@Override
public int compare(int[] o1, int[] o2) {
return Integer.compare(o1[0], o2[0]);
}
}

public int[][] merge(int[][] intervals) {
ArrayList<int[]> tmpList = new ArrayList<>();
int[][] result =new int[intervals.length][2];
if(intervals.length == 0) return result;
Collections.addAll(tmpList,intervals);
tmpList.sort(new MyComparable());
int i = 0;
result[0] = tmpList.get(0);
for(int j=1;j<tmpList.size();j++){
if(tmpList.get(j)[0] <= result[i][1]){
result[i][1] = tmpList.get(j)[1];
}else{
result[++i] = tmpList.get(j);
}
}
return Arrays.copyOf(result, i++);
}

}
11 changes: 11 additions & 0 deletions sort/sort.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="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
3 changes: 3 additions & 0 deletions sort/src/InsertSort.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
public class InsertSort {

}

0 comments on commit 3ce1aea

Please sign in to comment.