Tag: multithreading

java中的时间同步

在for-loop中,我通过检索和处理车辆信息来控制基于模拟步骤的交通模拟器SUMO。 为了确保我的程序在“实时”模拟(1个模拟步骤= 1秒),我想在处理阶段之后睡眠我的程序,直到下一个时间步骤开始。 为了获得更好的结果,我正在根据最初采用的参考时间戳计算时间戳。 循环看起来像这样: System.out.println(“start of traffic simulation …”); for (int i = 0; i < stepCount; i++) { System.out.println("step: " + i); // set before timeStamp beforeTimeStamp = System.currentTimeMillis(); if (firstStep) { // get reference timeStamp referenceTimeStamp = beforeTimeStamp; firstStep = false; } else { // get next vehicleVector vehicleVector = masterControl.traCIclient.simulateStep(); } […]

在Vaadin 7中调用VaadinSession getAttribute时需要锁定

我知道调用setAttribute( link )时有必要,但getAttirbute呢? 它是否正确? public Object getMyAttribute() { return VaadinSession.getCurrent().getAttribute(“myAttribute”); } 还是需要锁定? public Object getMyAttribute() { try { VaadinSession.getCurrent().getLockInstance().lock(); return VaadinSession.getCurrent().getAttribute(“myAttribute”); } finally { VaadinSession.getCurrent().getLockInstance().unlock(); } }

newFixedThreadPool()vs newCachedThreadPool()

如果newCachedThreadPool()根据需要创建一个线程池,根据需要创建新线程,但是在它们可用时将重用先前构造的线程,而在newFixedThreadPool(int size)情况下,指定大小以创建指定大小的线程池。 为什么newFixedThreadPool(int size)不是以newCachedThreadPool()方式实现的,其中线程池仅在需要时创建新线程并将线程限制为大小? 对上述内容的任何澄清都非常有用。

我用线程迷失了我的思绪

我想要这个类的对象: public class Chromosome implements Runnable, Comparable { private String[] chromosome; public double fitness; private Random chromoGen; public Chromosome(double[] candidate) { super(); //encode candidate PER parameter; using Matrix as storage chromosome = encode(candidate); chromoGen = new Random(); } //De-fault public Chromosome() { super(); chromoGen = new Random(); //de-fault genotype chromosome = new String[6]; } /** […]

Java月计时器

我正在尝试创建一个Timer / TimerTask,它将在每个月的同一天运行。 我无法安排重复计时器,因为一个月不会总是相同的时间长度。 所以,这是我的解决方案: public class MyTask extends TimerTask { public void run(){ //do process file stuff if(scheduledExecutionTime() != 0){ TimerHelper.restartMyTimer(); } } } public class TimerHelper { public static HashTable timersTable = new HashTable(); public static void restartMyTimer(){ Calendar runDate = Calendar.getInstance(); runDate.set(Calendar.DAY_OF_MONTH, 1); runDate.set(Calendar.HOUR_OF_DAY, 4); runDate.set(Calendar.MINUTE, 0); runDate.add(Calendar.MONTH, 1);//set to next month […]

顺序一致性易变解释

我正在观看来自java jpoint会议的video。 我对来自Alexey Shipilev报告的幻灯片有疑问: 幻灯片上的非英语版请原谅。 实际上作者说变量集是不可能的 r1 = 1 (Y) r2 = 0 (x) r3 = 1 (x) r4 = 0 (Y) 根据video,他暗示这显然是。 根据JMM,有人可以澄清为什么这个价值设定不可能? PS 如果我理解Alexey符号正确,则尊重以下代码: public class SequentialConsistency { static volatile int x; static volatile int y; public static void main(String[] args) { new Thread(new Runnable() { @Override public void run() { x = […]

在退出之前恢复中断的不可取消任务

我正在阅读一些java线程中断,我不明白一些东西。 希望有人能解释我。 所以,它完成了以下代码 public Integer getInteger(BlockingQueue queue) { boolean interrupted = false; try { while (true) { try { return queue.take(); } catch (InterruptedException e) { interrupted = true; // fall through and retry } } } finally { if (interrupted) Thread.currentThread().interrupt(); } } 解释如下: 不支持取消但仍调用可中断阻塞方法的活动必须在循环中调用它们,并在检测到中断时重试。 在这种情况下,他们应该在本地保存中断状态,并在返回之前恢复它,如清单所示。 而不是立即捕获InterruptedException。 过早设置中断状态可能会导致无限循环,因为大多数可中断阻塞方法会在进入时检查中断状态,并在设置时立即抛出InterruptedException。 (可中断方法通常在阻止或执行任何重要工作之前轮询中断,以便尽可能地响应中断。) 我不明白为什么我们应该在本地保存中断状态。 我很高兴听到一些解释。

Web应用程序中的Spring线程

我正在为MMO浏览器游戏编写服务器,我需要制作一些线程。 他们将一直在运行,有一些睡眠时间。 使用像这样的弹簧线是不是一个好主意? @Component @Scope(“prototype”) public class PrintTask2 implements Runnable{ String name; public void setName(String name){ this.name = name; } @Override public void run() { System.out.println(name + ” is running”); try { Thread.sleep(5000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(name + ” is running”); } } 将任务执行器实现为bean? 此外,线程以单例也启动,也被定义为bean。 我的做法有什么不对?

当使用单线程Executor时,为什么“header.get()+ footer.get()”会导致死锁?

这是Java Concurrency In Practice中的 8.1列表: public class ThreadDeadlock { ExecutorService exec = Executors.newSingleThreadExecutor(); public class RenderPageTask implements Callable { public String call() throws Exception { Future header, footer; header = exec.submit(new LoadFileTask(“header.html”)); footer = exec.submit(new LoadFileTask(“footer.html”)); String page = renderBody(); //Will deadlock — task waiting for result of subtask return header.get() + page + footer.get(); […]

Java中的多个对象锁?

锁定私有字段变量(而不是使用锁定对象)是安全/可接受的做法吗? 这样,我可以有不同的锁用于不同的目的。 示例如下: class Test { private Integer x = 0; private Integer y = 0; public void incrementX() { synchronized(x) { x++; } } public void decrementX() { synchronized(x) { x++; } } public void incrementY() { synchronized(y) { y++; } } public void decrementY() { synchronized(y) { y++; } } 或者我应该为每个想要锁定的私人会员拥有一个锁定对象? 例: class […]