Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions liuxin/knowledge-point/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.coderising</groupId>
<artifactId>knowledge-point</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>knowledge-point</name>
<url>http://maven.apache.org</url>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>

</dependencies>
<repositories>
<repository>
<id>aliyunmaven</id>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
</repository>
</repositories>
</project>
18 changes: 18 additions & 0 deletions liuxin/knowledge-point/src/main/java/cas/CASSequence.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package cas;

import java.util.concurrent.atomic.AtomicInteger;

public class CASSequence{

private AtomicInteger count = new AtomicInteger(0);

public int next(){
while(true){
int current = count.get();
int next = current +1;
if(count.compareAndSet(current, next)){
return next;
}
}
}
}
34 changes: 34 additions & 0 deletions liuxin/knowledge-point/src/main/java/cas/NoBlockingStack.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package cas;

import java.util.concurrent.atomic.AtomicReference;

public class NoBlockingStack<E> {
static class Node<E> {
final E item;
Node<E> next;
public Node(E item) { this.item = item; }
}

AtomicReference<Node<E>> head = new AtomicReference<Node<E>>();

public void push(E item) {
Node<E> newHead = new Node<E>(item);
Node<E> oldHead;
do {
oldHead = head.get();
newHead.next = oldHead;
} while (!head.compareAndSet(oldHead, newHead));
}
public E pop() {
Node<E> oldHead;
Node<E> newHead;
do {
oldHead = head.get();
if (oldHead == null)
return null;
newHead = oldHead.next;
} while (!head.compareAndSet(oldHead,newHead));
return oldHead.item;
}

}
11 changes: 11 additions & 0 deletions liuxin/knowledge-point/src/main/java/cas/Sequence.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package cas;

public class Sequence{

private int value;

public int next(){
return value ++;
}

}
20 changes: 20 additions & 0 deletions liuxin/knowledge-point/src/main/java/threadlocal/Context.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package threadlocal;
import java.util.HashMap;
import java.util.Map;

public class Context {

private static final ThreadLocal<String> txThreadLocal
= new ThreadLocal<String>();

public static void setTransactionID(String txID) {
txThreadLocal.set(txID);

}

public static String getTransactionId() {
return txThreadLocal.get();
}

}

Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package threadlocal;

public class TransactionManager {
private static final ThreadLocal<String> context = new ThreadLocal<String>();

public static void startTransaction() {
// logic to start a transaction
// ...
String txID = null;
context.set(txID);
}

public static String getTransactionId() {
return context.get();
}

public static void endTransaction() {
// logic to end a transaction
// …
context.remove();
}
}
35 changes: 35 additions & 0 deletions liuxin/knowledge-point/src/main/java/threadpool/BlockingQueue.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package threadpool;
import java.util.LinkedList;
import java.util.List;

public class BlockingQueue {

private List queue = new LinkedList();
private int limit = 10;

public BlockingQueue(int limit) {
this.limit = limit;
}

public synchronized void enqueue(Object item) throws InterruptedException {
while (this.queue.size() == this.limit) {
wait();
}
if (this.queue.size() == 0) {
notifyAll();
}
this.queue.add(item);
}

public synchronized Object dequeue() throws InterruptedException {
while (this.queue.size() == 0) {
wait();
}
if (this.queue.size() == this.limit) {
notifyAll();
}

return this.queue.remove(0);
}

}
5 changes: 5 additions & 0 deletions liuxin/knowledge-point/src/main/java/threadpool/Task.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package threadpool;

public interface Task {
public void execute();
}
37 changes: 37 additions & 0 deletions liuxin/knowledge-point/src/main/java/threadpool/ThreadPool.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package threadpool;
import java.util.ArrayList;
import java.util.List;


public class ThreadPool {

private BlockingQueue taskQueue = null;
private List<WorkerThread> threads = new ArrayList<WorkerThread>();
private boolean isStopped = false;

public ThreadPool(int numOfThreads, int maxNumOfTasks){
taskQueue = new BlockingQueue(maxNumOfTasks);

for(int i=0; i<numOfThreads; i++){
threads.add(new WorkerThread(taskQueue));
}
for(WorkerThread thread : threads){
thread.start();
}
}

public synchronized void execute(Task task) throws Exception{
if(this.isStopped) throw
new IllegalStateException("ThreadPool is stopped");

this.taskQueue.enqueue(task);
}

public synchronized void stop(){
this.isStopped = true;
for(WorkerThread thread : threads){
thread.doStop();
}
}

}
33 changes: 33 additions & 0 deletions liuxin/knowledge-point/src/main/java/threadpool/WorkerThread.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package threadpool;


public class WorkerThread extends Thread {

private BlockingQueue taskQueue = null;
private boolean isStopped = false;

public WorkerThread(BlockingQueue queue){
taskQueue = queue;
}

public void run(){
while(!isStopped()){
try{
Task task = (Task) taskQueue.dequeue();
task.execute();
} catch(Exception e){
//log or otherwise report exception,
//but keep pool thread alive.
}
}
}

public synchronized void doStop(){
isStopped = true;
this.interrupt(); //break pool thread out of dequeue() call.
}

public synchronized boolean isStopped(){
return isStopped;
}
}