Skip to content

Commit

Permalink
重命名
Browse files Browse the repository at this point in the history
  • Loading branch information
Viscent committed Sep 3, 2018
1 parent 1f8fa12 commit b826076
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,30 +18,32 @@
import java.util.concurrent.atomic.AtomicInteger;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class MemoryPseduoLeakingServlet extends HttpServlet {
private static final long serialVersionUID = 1L;

private final static ThreadLocal<AtomicInteger> TL_COUNTER=new ThreadLocal<AtomicInteger>(){
@Override
protected AtomicInteger initialValue() {
return new AtomicInteger();
}

};

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {


PrintWriter pwr = resp.getWriter();
pwr.write(TL_COUNTER.get().getAndIncrement());
pwr.close();

}
@WebServlet("/pseudo-leak")
public class MemoryPseudoLeakingServlet extends HttpServlet {
private static final long serialVersionUID = 1L;

final static ThreadLocal<AtomicInteger> TL_COUNTER =
new ThreadLocal<AtomicInteger>() {
@Override
protected AtomicInteger initialValue() {
return new AtomicInteger();
}

};

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {

PrintWriter pwr = resp.getWriter();
pwr.write(String.valueOf(TL_COUNTER.get().getAndIncrement()));
pwr.close();

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package io.github.viscent.mtpattern.ch6.promise.example;

import java.io.File;

public interface FTPUploader {
void init(String ftpServer, String ftpUserName, String password,
String serverDir) throws Exception;

void upload(File file) throws Exception;

void disconnect();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package io.github.viscent.mtpattern.ch6.promise.example;

import java.util.concurrent.Callable;
import java.util.concurrent.Executor;
import java.util.concurrent.Future;
import java.util.concurrent.FutureTask;

// 模式角色:Promise.Promisor
public class FTPUploaderPromisor {

// 模式角色:Promise.Promisor.compute
public static Future<FTPUploader> newFTPUploaderPromise(String ftpServer,
String ftpUserName, String password, String serverDir) {
Executor helperExecutor=new Executor() {
@Override
public void execute(Runnable command) {
Thread t = new Thread(command);
t.start();
}
};
return newFTPUploaderPromise(ftpServer,ftpUserName,password,serverDir,helperExecutor);
}

public static Future<FTPUploader> newFTPUploaderPromise(String ftpServer,
String ftpUserName, String password, String serverDir,
Executor helperExecutor) {
Callable<FTPUploader> callable = new Callable<FTPUploader>() {

@Override
public FTPUploader call() throws Exception {
String implClazz = System.getProperty("ftp.client.impl");
if (null == implClazz) {
implClazz =
"io.github.viscent.mtpattern.ch6.promise.example.FTPClientUtil";
}
FTPUploader self;
self = (FTPUploader) Class.forName(implClazz).newInstance();
// self = new FTPClientUtil();
self.init(ftpServer, ftpUserName, password, serverDir);
return self;
}

};

// task相当于模式角色:Promise.Promise
final FutureTask<FTPUploader> task = new FutureTask<>(callable);
helperExecutor.execute(task);
return task;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package io.github.viscent.mtpattern.ch6.promise.example;

import java.io.File;

import io.github.viscent.util.Debug;
import io.github.viscent.util.Tools;

public class FakeFTPUploader implements FTPUploader {
@Override
public void upload(File file) throws Exception {
Debug.info("uploading %s", file);
// 模拟文件上传所需的耗时
Tools.randomPause(100, 50);
}

@Override
public void disconnect() {
// 什么也不做
}

@Override
public void init(String ftpServer, String ftpUserName, String password,
String serverDir) throws Exception {
// 什么也不做

}
}

0 comments on commit b826076

Please sign in to comment.