AtomicLong
Spring
package com.uniubi.management.controller; import java.util.concurrent.atomic.AtomicLong; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController; import com.uniubi.management.model.Greeting; @RestController
public class GreetingController { private static final String template = "Hello,%s"; private final AtomicLong counter = new AtomicLong(); //http://localhost:8080/management/greeting.do
@RequestMapping("/greeting")
public Greeting greeting(@RequestParam(value = "name", defaultValue = "World") String name) {
return new Greeting(counter.getAndIncrement(), String.format(template, name));
}
}
package com.uniubi.management.controller; import java.io.IOException;
import java.io.PrintWriter; import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import org.springframework.http.ResponseEntity;
import org.springframework.http.client.HttpComponentsAsyncClientHttpRequestFactory;
import org.springframework.util.concurrent.ListenableFuture;
import org.springframework.util.concurrent.ListenableFutureCallback;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.AsyncRestTemplate; import com.alibaba.fastjson.JSON;
import com.uniubi.management.model.Greeting;
import com.uniubi.management.util.HttpClientUtil; @RestController
@RequestMapping("/api")
public class ApiController { // private static final String template = "Hello,%s"; // private final AtomicLong counter = new AtomicLong(); private AsyncRestTemplate template; @PostConstruct
public void init() {
System.out.println("-----------------ApiController init");
template = new AsyncRestTemplate();
template.setAsyncRequestFactory(new HttpComponentsAsyncClientHttpRequestFactory());
} @PreDestroy
public void destory() {
System.out.println("-----------------ApiController destory"); }
//http://localhost:8080/management/greeting.do
@RequestMapping("/test")
public Greeting test(@RequestParam(value = "username", defaultValue = "World") String username) throws IOException {
String result = HttpClientUtil.doGet("http://localhost:8080/management/greeting.do");
return JSON.parseObject(result, Greeting.class);
} @RequestMapping("/test2")
public void test2(@RequestParam(value = "username", defaultValue = "World") String username,
HttpServletRequest request, HttpServletResponse response) throws IOException {
// 调用完后立即返回(没有阻塞)
ListenableFuture<ResponseEntity<Greeting>> future = template.getForEntity("http://localhost:8080/management/greeting.do",
Greeting.class);
// 设置异步回调
future.addCallback(new ListenableFutureCallback<ResponseEntity<Greeting>>() {
@Override
public void onSuccess(ResponseEntity<Greeting> result) {
System.out.println("----"+JSON.toJSONString(result.getBody()));
try {
PrintWriter writer = response.getWriter();
writer.write(JSON.toJSONString(result.getBody()));
writer.flush();
} catch (IOException e) {
e.printStackTrace();
}
/*
finally{
try {
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
*/
} @Override
public void onFailure(Throwable t) {
try {
PrintWriter writer = response.getWriter();
writer.write("{a:1}");
writer.flush();
} catch (IOException e) {
e.printStackTrace();
}
/*
finally {
try {
writer.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
*/
}
});
}
}
package com.uniubi.management.controller; import org.springframework.http.ResponseEntity;
import org.springframework.util.concurrent.ListenableFuture;
import org.springframework.util.concurrent.ListenableFutureCallback;
import org.springframework.web.client.AsyncRestTemplate; import com.uniubi.management.model.Greeting; public class GreetingControllerTest {
public static void main(String[] args) {
AsyncRestTemplate template = new AsyncRestTemplate();
// 调用完后立即返回(没有阻塞)
ListenableFuture<ResponseEntity<Greeting>> future = template.getForEntity("http://localhost:8080/management/greeting.do",
Greeting.class);
// 设置异步回调
future.addCallback(new ListenableFutureCallback<ResponseEntity<Greeting>>() {
@Override
public void onSuccess(ResponseEntity<Greeting> result) {
System.out.println("======client get result : " + result.getBody());
} @Override
public void onFailure(Throwable t) {
System.out.println("======client failure : " + t);
}
});
System.out.println("==no wait");
}
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jaxws="http://cxf.apache.org/jaxws" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://cxf.apache.org/jaxws
http://cxf.apache.org/schemas/jaxws.xsd">
<import resource="classpath:META-INF/cxf/cxf.xml" />
<import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
<import resource="classpath:META-INF/cxf/cxf-extension-xml.xml" />
<bean id="getInfoServiceImpl" class="com.uniubi.management.ws.impl.GetInfoServiceImpl"></bean>
<jaxws:endpoint id="getInfoService" implementor="#getInfoServiceImpl"
address="/getInfoService"></jaxws:endpoint>
</beans>
AtomicLong的更多相关文章
- Java多线程系列--“JUC原子类”02之 AtomicLong原子类
概要 AtomicInteger, AtomicLong和AtomicBoolean这3个基本类型的原子类的原理和用法相似.本章以AtomicLong对基本类型的原子类进行介绍.内容包括:Atomic ...
- 多线程爬坑之路-J.U.C.atomic包下的AtomicInteger,AtomicLong等类的源码解析
Atomic原子类:为基本类型的封装类Boolean,Integer,Long,对象引用等提供原子操作. 一.Atomic包下的所有类如下表: 类摘要 AtomicBoolean 可以用原子方式更新的 ...
- [JDK8]性能优化之使用LongAdder替换AtomicLong
如果让你实现一个计数器,有点经验的同学可以很快的想到使用AtomicInteger或者AtomicLong进行简单的封装. 因为计数器操作涉及到内存的可见性和线程之间的竞争,而Atomic***的实现 ...
- JDK1.8 LongAdder 空间换时间: 比AtomicLong还高效的无锁实现
我们知道,AtomicLong的实现方式是内部有个value 变量,当多线程并发自增,自减时,均通过CAS 指令从机器指令级别操作保证并发的原子性. // setup to use Unsafe.co ...
- java多线程之AtomicLong与LongAdder
AtomicLong简要介绍 AtomicLong是作用是对长整形进行原子操作,显而易见,在java1.8中新加入了一个新的原子类LongAdder,该类也可以保证Long类型操作的原子性,相对于At ...
- 使用AtomicLong,经典银行账户问题
1.新建Account类,使用AtomicLong定义账户余额,增加和减少金额方法使用getAndAdd方法. package com.xkzhangsan.atomicpack.bank; impo ...
- AtomicLong和LongAdder的区别
AtomicLong的原理是依靠底层的cas来保障原子性的更新数据,在要添加或者减少的时候,会使用死循环不断地cas到特定的值,从而达到更新数据的目的. LongAdder在AtomicLong的基础 ...
- 【Java多线程】AtomicLong和LongAdder
AtomicLong简要介绍 AtomicLong是作用是对长整形进行原子操作,显而易见,在java1.8中新加入了一个新的原子类LongAdder,该类也可以保证Long类型操作的原子性,相对于At ...
- AtomicLong.lazySet 是如何工作的?
原文:http://www.quora.com/Java-programming-language/How-does-AtomicLong-lazySet-work Jackson Davis说:为一 ...
随机推荐
- android 发送自定义广播以及接收自定义广播
发送自定义广播程序: 布局文件: <?xml version="1.0" encoding="utf-8"?> <LinearLayout x ...
- http://blog.csdn.net/bluejoe2000/article/details/39521405#t9
http://blog.csdn.net/bluejoe2000/article/details/39521405#t9
- CSS margin 属性
设置外边距的最简单的方法就是使用 margin 属性. margin 属性接受任何长度单位,可以是像素.英寸.毫米或 em. margin 可以设置为 auto.更常见的做法是为外边距设置长度值.下面 ...
- .htaccess文件的详解以及404页面的设置
打开记事本,写入以下代码: ErrorDocument 404 /404.html保存成.htaccess文件上传到网站的根目录. /404.html是目录名和文件名,可以改成自己的名字.QUOTE: ...
- file_operations结构2
对与应用层的每个系统调用,驱动程序都有一个与之对应的函数.对于字符设备驱动程序,这些函数集合在一个file_operations类型的数据结构中,该结构体在Linux内核的include/linux/ ...
- 【HDOJ】2155 小黑的镇魂曲
线段树+SPFA最短路可以过.或者DP也能过.需要注意的是xl的范围是错的,测试用例中xl可能为0,他妈的,因为这个一直莫名其妙的wa.1. spfa建图增加一倍的点即可(讨论左端点和右端点). /* ...
- hadoop2.2编程:各种API
hadoop2.2 API http://hadoop.apache.org/docs/r0.23.9/api/index.html junit API http://junit.org/javado ...
- RAC SCAN
在介绍SCAN之前, 先简单的回顾一下ORACLE 关于IP 地址的命名用, 在9I RAC时, ORACLE没有自己的CLUSTERWARE, 主要依靠第三方的集群软件(如HACMP等), 客户端主 ...
- ReentrantReadWriteLock读写锁的使用
Lock比传统线程模型中的synchronized方式更加面向对象,与生活中的锁类似,锁本身也应该是一个对象.两个线程执行的代码片段要实现同步互斥的效果,它们必须用同一个Lock对象. 读写锁:分为读 ...
- MVVM Light中的Message
比喻:像漂流瓶一样发送一个Message,任何人有兴趣就可以拾起来. MVVM Light中的Message的使用分为三个步骤: 1.创建一个类,包含要传递的Message. 2.在ViewModel ...