易截截图软件、单文件、免安装、纯绿色、仅160KB

java并发编程实践 笔记(1)

线程安全 什么是线程安全(thread-safe)? 一个类,如果能够在多线程并发访问的环境下(不管线程访问的顺序)正确表现自己的行为,并且无需外部调用添加任何同步之类的操作,这个类就是线程安全的。
这个正确性可以这么理解,就是说多线程访问的结果不会不同于单线程的访问。
线程安全的类不需要外部调用提供任何附加的同步。 无状态(stateless)的类永远是线程安全的。
什么是无状态的类?没有实例变量(field),也没有引用其它类的field。 原子性 A race condition occurs when the correctness of a computation depends on the relative timing or interleaving of multiple threads by the runtime; in other words, when getting the right answer relies on lucky timing. 最常见的race condition是check-and-act。要防止这种race condition,我们需要原子性的操作。什么是原子性呢,就是说,你这个操作在执行的过程中,对于你操作的状态上其它的操作(包括你自己)要么全都执行完了,要么还没开始。
想说句通顺的中国话怎么这么难啊,还是给出原文吧: Operations A and B are atomic with respect to each other if, from the perspective of a thread executing A, when another thread executes B, either all of B has executed or none of it has. An atomic operation is one that is atomic with respect to all operations, including itself, that operate on the same state. 来个例子 @NotThreadSafe
public class UnsafeCountingFactorizer implements Servlet {
private long count = 0;
public long getCount() { return count; }
public void service(ServletRequest req, ServletResponse resp) {
BigInteger i = extractfromRequest(req);
BigInteger[] factors = factor(i);
++count;
encodeIntoResponse(resp, factors);
}
}
@ThreadSafe
public class CountingFactorizer implements Servlet {
private final AtomicLong count = new AtomicLong(0);
public long getCount() { return count.get(); }
public void service(ServletRequest req, ServletResponse resp) {
BigInteger i = extractfromRequest(req);
BigInteger[] factors = factor(i);
count.incrementAndGet();


相关文档:

传智播客Java培训 HTML基础

今天是写第一篇传智播客教程日志,也是我看
JavaScript
视频的第一天。
先说缘由吧。大三还有三个月就要结束了,可我还没有感觉到自己能够有足够的能力找到份好工作,大学期间学到的都是些皮毛和理论,我不想到了找工作的时候,面试官问我会
XXX
吗?我说不会。因此我想通过培训增加自己的竞争力。我报了本地的一家
I ......

[转]Does Java pass by reference or pass by value?

http://www.javaworld.com/javaworld/javaqa/2000-05/03-qa-0526-pass.html
Does Java pass by reference or pass by value?
Why can't you swap in Java?
By Tony
Sintes, JavaWorld.com, 05/26/00

Print
Email
Feedback
Resources
Discuss
(76)
Digg
Reddit
SlashDot
Stumble
......

java 错误代码提示

 //=============================输出奇数
public class OddTest {
public static boolean isOdd(int i){
return i % 2 != 0; //比较 i % 2 == 0;注: -1%2 = -1
}
public static void main(String[] args) {
for(int i = -10; i <= 10; i++) {
System.out.println(isOdd(i));
}
}
}
// ......

Java SWT 窗口居中对齐

public static void CentreWnd(Shell shell){
int width = shell.getMonitor().getClientArea().width;
int height = shell.getMonitor().getClientArea().height;
int x = shell.getSize().x;
int y = shell.getSize().y;
if (x > width) {
shell.getSize().x = width;
}
if (y > height) ......

Java单例模式


 1、饿汉式
  package singleton;
  /**
  * 饿汉式单例
  * @author 蒋明原
  *
  */
  public class HungrySingleton {
  /**jvm保证instance只被初始化一次*/
  private static HungrySingleton instance = new HungrySingleton();
  /**阻止外部使用new实例化对象*/
  private Hun ......
© 2009 ej38.com All Rights Reserved. 关于E健网联系我们 | 站点地图 | 赣ICP备09004571号