Skip to content
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;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.coderising.ood.srp.good.template;

public interface MailBodyTemplate {
public String render();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.coderising.ood.srp.good.template;

import java.util.Map;

public class TextMailBodyTemplate implements MailBodyTemplate {

private Map<String,String >paramMap ;

public TextMailBodyTemplate(Map<String,String> map){
paramMap = map;
}

@Override
public String render() {
//使用某种模板技术实现Render
return null;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.coderising.ood.srp.good1;
import java.util.HashMap;
import java.util.Map;

public class Configuration {

static Map<String,String> configurations = new HashMap<>();
static{
configurations.put(ConfigurationKeys.SMTP_SERVER, "smtp.163.com");
configurations.put(ConfigurationKeys.ALT_SMTP_SERVER, "smtp1.163.com");
configurations.put(ConfigurationKeys.EMAIL_ADMIN, "admin@company.com");
}
/**
* 应该从配置文件读, 但是这里简化为直接从一个map 中去读
* @param key
* @return
*/
public String getProperty(String key) {

return configurations.get(key);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.coderising.ood.srp.good1;

public class ConfigurationKeys {

public static final String SMTP_SERVER = "smtp.server";
public static final String ALT_SMTP_SERVER = "alt.smtp.server";
public static final String EMAIL_ADMIN = "email.admin";

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.coderising.ood.srp.good1;

import java.util.List;

import com.coderising.ood.srp.good.template.MailBodyTemplate;

public class Mail {

private User user;

public Mail(User u){
this.user = u;
}
public String getAddress(){
return user.getEMailAddress();
}
public String getSubject(){
return "您关注的产品降价了";
}
public String getBody(){

return "尊敬的 "+user.getName()+", 您关注的产品 " + this.buildProductDescList() + " 降价了,欢迎购买!" ;
}
private String buildProductDescList() {
List<Product> products = user.getSubscribedProducts();
//.... 实现略...
return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.coderising.ood.srp.good1;

public class MailSender {

private String fromAddress ;
private String smtpHost;
private String altSmtpHost;

public MailSender(Configuration config){
this.fromAddress = config.getProperty(ConfigurationKeys.EMAIL_ADMIN);
this.smtpHost = config.getProperty(ConfigurationKeys.SMTP_SERVER);
this.altSmtpHost = config.getProperty(ConfigurationKeys.ALT_SMTP_SERVER);
}

public void sendMail(Mail mail){
try{
sendEmail(mail,this.smtpHost);
}catch(Exception e){
try{
sendEmail(mail,this.altSmtpHost);
}catch (Exception ex){
System.out.println("通过备用 SMTP服务器发送邮件失败: " + ex.getMessage());
}

}
}

private void sendEmail(Mail mail, String smtpHost){

String toAddress = mail.getAddress();
String subject = mail.getSubject();
String msg = mail.getBody();
//发送邮件
}
}
Loading