// 上下文类 public class Context { private String name; private long id
public long getId() { return id; }
public void setId(long id) { this.id = id; }
public String getName() { return this.name; }
public void setName(String name) { this.name = name; } }
// 设置上下文名字 public class QueryNameAction { public void execute(Context context) { try { Thread.sleep(1000L); String name = Thread.currentThread().getName(); context.setName(name); } catch (InterruptedException e) { e.printStackTrace(); } } }
// 设置上下文ID public class QueryIdAction { public void execute(Context context) { try { Thread.sleep(1000L); long id = Thread.currentThread().getId(); context.setId(id); } catch (InterruptedException e) { e.printStackTrace(); } } }
// 执行方法 public class ExecutionTask implements Runnable {
private QueryNameAction queryNameAction = new QueryNameAction(); private QueryIdAction queryIdAction = new QueryIdAction();
@Override public void run() { final Context context = new Context(); queryNameAction.execute(context); System.out.println("The name query successful"); queryIdAction.execute(context); System.out.println("The id query successful");
System.out.println("The Name is " + context.getName() + " and id " + context.getId()); } }
public static void main(String[] args) { IntStream.range(1, 5).forEach(i -> new Thread(new ContextTest().new ExecutionTask()).start()); } }
执行结果:
1 2 3 4 5 6 7 8 9 10 11 12
The name query successful The name query successful The name query successful The name query successful The id query successful The id query successful The id query successful The id query successful The Name is Thread-1 and id 11 The Name is Thread-2 and id 12 The Name is Thread-3 and id 13 The Name is Thread-0 and id 10
public class ContextTest { // 上下文类 public static class Context { private String name; private long id;
public long getId() { return id; }
public void setId(long id) { this.id = id; }
public String getName() { return this.name; }
public void setName(String name) { this.name = name; } }
// 复制上下文到ThreadLocal中 public final static class ActionContext {
private static final ThreadLocal<Context> threadLocal = new ThreadLocal<Context>() { @Override protected Context initialValue() { return new Context(); } };
public static ActionContext getActionContext() { return ContextHolder.actionContext; }
public Context getContext() { return threadLocal.get(); }
// 获取ActionContext单例 public static class ContextHolder { private final static ActionContext actionContext = new ActionContext(); } }
// 设置上下文名字 public class QueryNameAction { public void execute() { try { Thread.sleep(1000L); String name = Thread.currentThread().getName(); ActionContext.getActionContext().getContext().setName(name); } catch (InterruptedException e) { e.printStackTrace(); } } }
// 设置上下文ID public class QueryIdAction { public void execute() { try { Thread.sleep(1000L); long id = Thread.currentThread().getId(); ActionContext.getActionContext().getContext().setId(id); } catch (InterruptedException e) { e.printStackTrace(); } } }
// 执行方法 public class ExecutionTask implements Runnable { private QueryNameAction queryNameAction = new QueryNameAction(); private QueryIdAction queryIdAction = new QueryIdAction();
@Override public void run() { queryNameAction.execute();//设置线程名 System.out.println("The name query successful"); queryIdAction.execute();//设置线程ID System.out.println("The id query successful");
System.out.println("The Name is " + ActionContext.getActionContext().getContext().getName() + " and id " + ActionContext.getActionContext().getContext().getId()) } }
public static void main(String[] args) { IntStream.range(1, 5).forEach(i -> new Thread(new ContextTest().new ExecutionTask()).start()); } }
运行结果:
1 2 3 4 5 6 7 8 9 10 11 12
The name query successful The name query successful The name query successful The name query successful The id query successful The id query successful The id query successful The id query successful The Name is Thread-2 and id 12 The Name is Thread-0 and id 10 The Name is Thread-1 and id 11 The Name is Thread-3 and id 13
//Socket启动服务 public class Server { private static int DEFAULT_PORT = 12345; private static ServerSocket server;
public static void start() throws IOException { start(DEFAULT_PORT); }
public static void start(int port) throws IOException { if (server != null) { return; } try { //启动服务 server = new ServerSocket(port); // 通过无线循环监听客户端连接 while (true) { Socket socket = server.accept(); // 当有新的客户端接入时,会执行下面的代码 long start = System.currentTimeMillis(); new Thread(new ServerHandler(socket)).start(); long end = System.currentTimeMillis();
System.out.println("Spend time is " + (end - start)); } } finally { if (server != null) { System.out.println("服务器已关闭。"); server.close(); }
}
} public static void main(String[] args) throws InterruptedException{
//机器人 public class Worker extends Thread{ private static final Random random = new Random(System.currentTimeMillis()); private final PackageChannel channel;