Tag: 并发

如果Collection只写在构造函数中,那么它是一个线程安全吗?

假设我们有这门课 final class Foo { private final Set bar = new HashSet(); public Foo() { bar.add(“one”); bar.add(“two”); bar.add(“three”); } public boolean contains(final String s) { return bar.contains(s); } } 将实例化Foo并从多个线程调用contains此对象是不是线程安全的? 对该集合的引用是private和final 。 没有人可以直接访问该集合。 唯一的写访问权限发生在构造函数中 执行构造函数后,集合将只读取而不是修改。 如果没有,是否有一个纯Java替代Guava的不可变集合?

System.out.format如何防止死锁?

我发现在经典Java死锁教程中包含对System.out.format的调用可以防止发生死锁,我无法弄清楚原因。 下面的代码与教程的代码相同,添加了System.out.format(“Hi, I’m %s…no deadlock for you!\n\n”, alphonse.getName()); main内容System.out.format(“Hi, I’m %s…no deadlock for you!\n\n”, alphonse.getName()); public class Deadlock { static class Friend { private final String name; public Friend(String name) { this.name = name; } public String getName() { return this.name; } public synchronized void bow(Friend bower) { System.out.format(“%s: %s has bowed to me!\n”, this.name, […]

启动/暂停/恢复/暂停…由其他类调用的方法

我想实现一个Anytime k-NN分类器,但我找不到一种方法来调用“classify(…)”方法一段特定的时间,暂停它,在方法暂停之前获取可用的结果,恢复特定时间的方法,暂停它,在方法暂停之前获得可用的结果,等等…我使用数据结构来获得近似结果。 当算法遍历数据结构时,它最终会遇到实际的训练数据向量。 public class AnytimeKNN{ public int classify(queryPoint, k){ class_label; 1. Assign an initial value to ‘class_label’. 2.while(not actual training data vectors are encountered){ 1. traverse the data structure 2. assign a new value to ‘class_label’ } } } 我想以下列方式从main方法调用’classify(..)’方法: 启动方法’classify(..)’ 当分配初始值为’class_label’时,暂停方法’classify(..)’。 获取初始标签 在X时间内继续使用方法’classify(..)’ 暂停方法’classify(..)’ 获取新的“class_label” 恢复方法’分类(..)’的X时间等等…… 提前致谢!

对象创建(状态初始化)和线程安全

我正在研究“实践中的Java并发”这本书,发现在下面引用的声明中很难相信(但不幸的是它有意义)。 http://www.informit.com/store/java-concurrency-in-practice-9780321349606 只是想清楚这100% public class Holder { private int n; public Holder(int n) { this.n = n; } public void assertSanity() { if (n != n) throw new AssertionError(“This statement is false.”); } } 虽然在构造函数中设置的字段值可能看起来是写入这些字段的第一个值,因此没有“较旧”的值可以看作是过时值, 但是在子类构造函数运行之前 , Object构造函数首先将默认值写入所有字段。 因此,可以将字段的默认值视为过时值 关于上面的粗体陈述, 我知道行为但现在很明显,这个构造函数的调用层次结构并不保证是ATOMIC(在锁定保护的单个同步块中调用超级构造函数),但是什么是解决方案? 想象一个具有多个级别的类层次结构(即使不推荐,也可以假设它是可能的)。 上面的代码片段是我们在大多数项目中每天都看到的一种原型。

灵活的锁定方式(选择性锁定)

我需要解决具有不同内存位置的相等对象的情况(由于multithreading,它发生在REST请求中)。 因此作为部分解决方案,我实施了服务。 我在这里分享最重要的部分: private Map lockHolder = new HashMap(); void unLock(T monitorMarker) { synchronized (lockHolder) { ReentrantLock lock = lockHolder.get(monitorMarker); if (lock == null || lock.getHoldCount() == 0) { return; } lock.unlock(); if (lock.getHoldCount() == 0) { lockHolder.remove(monitorMarker); } } } ReentrantLock getLockForCalendar(T monitorMarker) { synchronized(monitorMarker) { ReentrantLock lock = lockHolder.get(monitorMarker); if (lock == null) […]

HashMap持有重复键

在尝试使用HashMap ,我发现了一些奇怪的东西。 运行4个线程,每个线程尝试使用0到9999之间的键(键,值),值为常量字符串。 完成所有线程后, map.size()返回大于10,000的值。 这怎么发生的? 这是否意味着地图包含重复的键? 我迭代了map.entrySet()并发现某些键的计数确实超过1.如果我在地图上为一个这样的键执行get() ,将返回什么值。 这是我试过的代码 final HashMap vals = new HashMap(16_383); Runnable task = new Runnable() { @Override public void run() { for (int i = 0; i < 10000; i++) { vals.put(""+i, Thread.currentThread().getName()); } } }; Thread thread = new Thread(task, "a"); Thread thread1 = new Thread(task, "b"); Thread […]

multithreading状态依赖问题

我得到以下代码片段: public class ThreadTest{ private static class Thread01 extends Thread{ private Thread02 _th2; public int foo = 0; public void setThrd02(Thread02 thrd2){ _th2 = thrd2; } public void run(){ try{ for(int i=0;i<10;i++) foo += i; synchronized(this){this.notify();}; synchronized(_th2){_th2.wait();}; System.out.print(" Foo: " + _th2.foo); }catch(InterruptedException ie){ ie.printStackTrace();} } } private static class Thread02 extends Thread{ private final […]

在写入文件时读取文件

我必须读取tomcat日志文件,过了一段时间(例如:一小时)我将再次读取该文件(仅用于新添加的内容),所以我创建了RandomAccessFile来记录我完成的最后一个位置,并使用BufferedReader.readLine()方法。 但是,我发现有时我无法读取整个文件行。 例如,tomcat正在编写以下内容(仅举例): 192.168.0.0 localhost /index.html ….. 在这一刻,当我读到时,我可能会得到结果: 192.168.0 0 localhost /index.html ….. 要么 192.168.0.0 localhost /index.html ….. 也就是说,如果正在编写此行,我的读者会读取不完整的行。 所以我想知道是否有任何想法来决定正在阅读的线路是否已经完成? 这是核心代码: raf = new RandomAccessFile(file, “r”); raf.seek(pos); while ((line = raf.readLine()) != null) { System.out.println(line); } pos = raf.getFilePointer(); raf.close(); 我试过这个(添加contidtion): raf = new RandomAccessFile(file, “r”); raf.seek(pos); while ((line = raf.readLine()) != null) { if(line.chartAt(line.length()-1)==’\n’){ […]

将JFrame与连续输入流一起使用

我正在尝试实现从我的redboard的串口读取的代码,并根据它读取的内容重新绘制一个圆圈。 这样做的最终目的是使用Robot类来实现实际的光标控制,但我首先想要了解更多有关Java的信息,因此我首先尝试使用一些基本图形来实现它。 总结一下我的问题,我不知道如何使用静态方法不断变化的输入来使用JFrame。 访问JAR的串口可以在http://fazecast.github.io/jSerialComm/找到 Arduino以“UpLeft”,“Up”,“UpRight”,“Left”,“Center”,“Right”,“DownLeft”,“Down”,“Down”,“Up”等forms连续写入基于FPGA加速度计系统的序列。彻头彻尾”。 然后Java程序应该抓住它并相应地重新绘制一个圆圈。 我能够打开COMM3并打印从我的硬件收到的正确方向,但每当我尝试应用JFrame时,我都会迷路。 我发现了很多ActionListener教程,但是这个实现应该是连续的,不依赖于鼠标或键盘事件。 因此,我不知道如何使用paintComponent()和painter()方法,因为main方法是静态的。 非常感谢您的宝贵时间! import java.awt.Color; import java.awt.Graphics; import java.util.Scanner; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JSlider; import com.fazecast.jSerialComm.*; public class Main extends JPanel{ public void paintComponent(Graphics g, int x, int y) { super.paintComponent(g); g.setColor(Color.MAGENTA); g.fillOval(x, y, 20, 20); } public void painter(int x, int y, int velx, int vely){ […]

可以同时安全地访问ehcache diskstore吗?

我看到DiskStore位置有一个.lock文件。 我一直都明白,使文件IOprimefaces化是困难的,有时甚至是不可能的。 所以,我的期望是多个应用程序无法安全地使用相同的ehcache DiskStore。 但是,锁文件让我很奇怪。 除了性能瓶颈,它可以工作吗? 上下文:我想知道运行相同应用程序的两个Java Servlet上下文是否可以共享DiskStore。