Tag:

用Java并行/multithreading创建现有代码

我有一个非常简单的爬虫。 我想让我当前的代码在几个线程中运行。 你能给我一些教程或文章来帮助我实现这个考试吗? 我原来是.Net开发人员,在.Net中我在multithreading中运行代码没有任何问题,但遗憾的是我对Java中的线程一无所知。 我的爬虫是一个命令行软件,所以不用担心GUI。 先谢谢你。

AtomicInteger和volatile

我知道volatile允许可见性, AtomicInteger允许primefaces性。 因此,如果我使用易失性AtomicInteger ,是否意味着我不必再使用任何同步机制? 例如。 class A { private volatile AtomicInteger count; void someMethod(){ // do something if(count.get() < 10) { count.incrementAndGet(); } } 这线程安全吗?

尝试在runnable中同步方法

我有一个ConcurrentMap,它可以在我的runnables之外实例化,但是在runnables中共享和更新。 我的runnables需要是并发的,但我的concurrentMap更新需要同步以防止替换先前的entrys。 有人能告诉我我做错了什么。 public class ExecutionSubmitExample { public static void main(String[] args) { //Ten concurrent threads ExecutorService es = Executors.newFixedThreadPool(10); List<Future> tasks = new ArrayList(); ConcurrentHashMap concurrentMap = new ConcurrentHashMap(); for (int x = 0; x < 10; x++) { Example example = new Example(concurrentMap, x); Future future = es.submit(example, example); tasks.add(future); } try { […]

multithreading环境中的Hashmap和哈希表

我真的很困惑这两个集合在multithreading环境中的表现。 哈希表是同步的,这意味着没有2个线程同时更新它的值吗?

并发添加非线程安全的HashSet – 可能发生的最坏情况是什么?

情况: 多个线程只向非线程安全的java.util.HashSet添加值,并且在这些线程停止之前,不会对Set执行任何其他操作。 题: 可能发生的最坏情况是什么?

制作DateFormat Threadsafe。 使用什么,同步或线程本地

我想让以下代码线程安全。 实现它的最佳方法是什么? private static final DateFormat DATE_FORMAT = DateFormat.getDateTimeInstance(); public static final String eventTypeToDateTimeString(long timestamp) { return DATE_FORMAT.format(new Date(timestamp)); }

调用Thread.sleep()与*中断状态*设置?

Java文档在这一点上并不清楚。 如果在调用Thread.sleep() 之前调用Thread上的中断会发生什么: //interrupt reaches Thread here try { Thread.sleep(3000); } catch (InterruptedException e) { return; } 是否会抛出InterruptedException ? 请指向相关文档。

在Java中,如果每个线程写入单独的单元格空间,是否需要同步对数组的写访问?

如果每个线程写入单独的单元格空间,是否需要在Java中同步对数组的写访问权限? 编辑:具体来说,数组是原始数组或不可变对象数组。 防爆。 一个int数组或一个String数组。

Axis2生成的存根是否是线程安全的?

是否通过Axis2线程安全地从WSDL生成客户端存根? 当然,“线程安全”不是一个严格定义的术语,所以我至少对以下内容感兴趣: 不同线程可以同时访问同一存根类的不同实例,具有与单线程执行相同的有效行为吗? 同一个存根类的单个实例是否可以由不同的线程同时访问,与在单线程执行中以某种任意方式交错的相同调用具有相同的有效行为? 您可能还希望使用此处描述的术语(并在此处起源)来更精确地讨论这一术语。

为什么这个同步方法没有按预期工作?

有人可以解释两个我为什么这些代码不输出相同的结果(两个代码之间的唯一区别在于run()方法)? 注意:第一个代码似乎没有做任何锁定! 第一个代码: class LetterThread extends Thread { private StringBuffer letter; public static void main(String[] args) { StringBuffer sbltr = new StringBuffer(“A”); LetterThread one = new LetterThread(sbltr); LetterThread two = new LetterThread(sbltr); LetterThread three = new LetterThread(sbltr); one.setName(“Thread ONE”); two.setName(“Thread TWO”); three.setName(“Thread THREE”); one.start(); two.start(); three.start(); } LetterThread(StringBuffer letter) { this.letter = letter; } public […]