diff --git a/src/com/java/practice/producerConsumer/Consumer.java b/src/com/java/practice/producerConsumer/Consumer.java new file mode 100644 index 00000000..0fb834e1 --- /dev/null +++ b/src/com/java/practice/producerConsumer/Consumer.java @@ -0,0 +1,36 @@ +package com.java.practice.producerConsumer; + +import java.util.concurrent.BlockingQueue; + +/** + * Created by richard02.zhang on 18/2/7. + */ +public class Consumer implements Runnable { + + private final BlockingQueue blockingQueue; + + private volatile boolean flag; + + public Consumer(BlockingQueue blockingQueue) { + this.blockingQueue = blockingQueue; + this.flag = false; + } + + @Override + public void run() { + while (!flag) { + int info; + try { + info = blockingQueue.take(); + System.out.println(Thread.currentThread().getName()+" consumer "+info+"\n"); + Thread.sleep(50); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + } + + public void shutdown() { + flag = true; + } +} diff --git a/src/com/java/practice/producerConsumer/Producer.java b/src/com/java/practice/producerConsumer/Producer.java new file mode 100644 index 00000000..cc34ed6f --- /dev/null +++ b/src/com/java/practice/producerConsumer/Producer.java @@ -0,0 +1,40 @@ +package com.java.practice.producerConsumer; + +import java.util.Random; +import java.util.concurrent.BlockingQueue; + +/** + * Created by richard02.zhang on 18/2/7. + */ +public class Producer implements Runnable { + + private final BlockingQueue blockingQueue; + + private volatile boolean flag; + + private Random random; + + public Producer(BlockingQueue blockingQueue) { + this.blockingQueue = blockingQueue; + this.flag = false; + this.random = new Random(); + } + + @Override + public void run() { + while (!flag) { + int info = random.nextInt(100); + try { + blockingQueue.put(info); + System.out.println(Thread.currentThread().getName()+"producer "+info+"\n"); + Thread.sleep(50); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + } + + public void shutdown() { + flag = true; + } +} diff --git a/src/com/java/practice/producerConsumer/Worker.java b/src/com/java/practice/producerConsumer/Worker.java new file mode 100644 index 00000000..4bbd1cc8 --- /dev/null +++ b/src/com/java/practice/producerConsumer/Worker.java @@ -0,0 +1,71 @@ +package com.java.practice.producerConsumer; + +import java.util.Date; +import java.util.LinkedList; +import java.util.List; +import java.util.concurrent.BlockingQueue; +import java.util.concurrent.LinkedBlockingDeque; + +/** + * Created by richard02.zhang on 18/2/7. + * 生产者消费者模式 阻塞队列实现 + */ +public class Worker { + + private int maxSize; + + private List storage; + + public Worker(int maxSize) { + this.maxSize = maxSize; + this.storage = new LinkedList<>(); + } + + public synchronized void put() { + try { + while (storage.size() == maxSize) { + System.out.println(Thread.currentThread().getName()+": wait \n"); + wait(); + } + storage.add(new Date()); + System.out.println(Thread.currentThread().getName()+": put: "+storage.size()+"\n"); + notifyAll(); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + + public synchronized void take() { + try { + while (storage.size() == 0) { + System.out.println(Thread.currentThread().getName()+": wait \n"); + wait(); + } + Date d = ((LinkedList)storage).poll(); + System.out.println(Thread.currentThread().getName()+": d :" + d.toString() +": take: "+storage.size()+"\n"); + notifyAll(); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + + public static void main(String[] args) { + BlockingQueue blockingQueue = new LinkedBlockingDeque<>(10); + Producer producer = new Producer(blockingQueue); + Consumer consumer = new Consumer(blockingQueue); + for (int i = 0; i < 10; i++) { + if (i < 5) { + new Thread(producer, "producer-"+i).start(); + } else { + new Thread(consumer, "consumer-"+(i-5)).start(); + } + } + try { + Thread.sleep(1000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + producer.shutdown(); + consumer.shutdown(); + } +} diff --git a/src/com/java/practice/sort/BubbleSort.java b/src/com/java/practice/sort/BubbleSort.java new file mode 100644 index 00000000..51b5328e --- /dev/null +++ b/src/com/java/practice/sort/BubbleSort.java @@ -0,0 +1,34 @@ +package com.java.practice.sort; + +/** + * Created by richard02.zhang on 18/1/26. + */ +public class BubbleSort { + + private static void swap(int[] arr, int x, int y) { + int temp = arr[x]; + arr[x] = arr[y]; + arr[y] = temp; + } + + private static void bubbleSort(int[] arr) { + if (null == arr || 0 == arr.length) { + return; + } + for (int i = 0; i < arr.length; i++){ + for (int j = 0; j < arr.length; j++) { + if (arr[i] <= arr[j]) { + swap(arr, i, j); + } + } + } + } + + public static void main(String[] args) { + int[] arr = {4, 5, 2, 2, 7, 1, 9, 8}; + bubbleSort(arr); + for (int i = 0; i < arr.length; i++){ + System.out.println(arr[i]); + } + } +} diff --git a/src/com/java/practice/sort/QuickSort.java b/src/com/java/practice/sort/QuickSort.java new file mode 100644 index 00000000..1fe8fdcf --- /dev/null +++ b/src/com/java/practice/sort/QuickSort.java @@ -0,0 +1,45 @@ +package com.java.practice.sort; + +/** + * Created by richard02.zhang on 18/1/26. + */ +public class QuickSort { + + private static void swap(int[] arr, int x, int y) { + int temp = arr[x]; + arr[x] = arr[y]; + arr[y] = temp; + } + + private static int partition(int[] arr, int begin, int end) { + int index = begin; + int pivot = arr[end]; + for (int i = begin; i < arr.length; i++) { + if (arr[i] < pivot ) { + swap(arr, index, i); + index++; + } + } + swap(arr, end, index); + return index; + } + + private static void quickSort(int[] arr, int begin, int end) { + if (null == arr || arr.length == 0) { + return; + } + if (begin < end) { + int index = partition(arr, begin, end); + quickSort(arr, begin, index - 1); + quickSort(arr, index + 1, end); + } + } + + public static void main(String[] args) { + int[] arr = {5, 4, 4, 3, 3, 2, 1}; + quickSort(arr, 0, arr.length - 1); + for (int i = 0; i < arr.length; i++){ + System.out.println(arr[i]); + } + } +}