如果函数不更改任何共享对象但仅使用给定的参数,是否可以重用同一个Nashorn引擎和同一个JavaScriptObject(对于所有servlet请求,作为JS函数的评估)电话?请看以下示例:
public class MyServlet extends HttpServlet {
private ScriptEngineManager factory;
private ScriptEngine engine;
private ScriptObjectMirror script;
@Override
public void init() throws ServletException {
try {
factory = new ScriptEngineManager();
engine = factory.getEngineByName("nashorn");
script = (ScriptObjectMirror)engine.eval("function(writer) {writer.print('Hello, World!');}");
} catch (ScriptException ex) {
Logger.getLogger(MyServlet.class.getName()).log(Level.SEVERE, null, ex);
}
}
@Override
public void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException {
try (PrintWriter writer = res.getWriter()) {
script.call(null, writer);
writer.close();
} catch (IOException ex) {
Logger.getLogger(MyServlet.class.getName()).log(Level.SEVERE, null, ex);
}
}
Run Code Online (Sandbox Code Playgroud)
这是线程安全的吗?这是在Servlet中重用Nashorn ScriptEngine的后续内容 …
我正在使用一个在发送请求时需要回调的框架.每个回调都必须实现此接口.回调中的方法是异步调用的.
public interface ClientCallback<RESP extends Response>
{
public void onSuccessResponse(RESP resp);
public void onFailureResponse(FailureResponse failure);
public void onError(Throwable e);
}
Run Code Online (Sandbox Code Playgroud)
要使用TestNG编写集成测试,我想要一个阻塞回调.所以我使用CountDownLatch来在线程之间进行同步.
这里真的需要AtomicReference还是原始引用好吗?我知道如果我使用原始引用和原始整数(而不是CountDownLatch),代码将无法工作,因为无法保证可见性.但由于CountDownLatch已经同步,我不确定是否需要AtomicReference的额外同步.注意:Result类是不可变的.
public class BlockingCallback<RESP extends Response> implements ClientCallback<RESP>
{
private final AtomicReference<Result<RESP>> _result = new AtomicReference<Result<RESP>>();
private final CountDownLatch _latch = new CountDownLatch(1);
public void onSuccessResponse(RESP resp)
{
_result.set(new Result<RESP>(resp, null, null));
_latch.countDown();
}
public void onFailureResponse(FailureResponse failure)
{
_result.set(new Result<RESP>(null, failure, null));
_latch.countDown();
}
public void onError(Throwable e)
{
_result.set(new Result<RESP>(null, null, e));
_latch.countDown();
}
public Result<RESP> …Run Code Online (Sandbox Code Playgroud) 在受限制的设备上,我经常发现自己在2个线程与2个bool之间"伪造"锁定.每个只由一个线程读取,只由另一个线程写入.这就是我的意思:
bool quitted = false, paused = false;
bool should_quit = false, should_pause = false;
void downloader_thread() {
quitted = false;
while(!should_quit) {
fill_buffer(bfr);
if(should_pause) {
is_paused = true;
while(should_pause) sleep(50);
is_paused = false;
}
}
quitted = true;
}
void ui_thread() {
// new Thread(downloader_thread).start();
// ...
should_pause = true;
while(!is_paused) sleep(50);
// resize buffer or something else non-thread-safe
should_pause = false;
}
Run Code Online (Sandbox Code Playgroud)
当然在PC上我不会这样做,但在受限制的设备上,似乎读取一个bool值比获得锁定要快得多.当然,sleep(50)当需要更改缓冲区时,我需要权衡较慢的恢复(参见" ").
问题 - 它是完全线程安全的吗?或者在伪造这样的锁时我需要注意隐藏的陷阱吗?或者我应该不这样做?
即使经过这个,我仍然不清楚最终的使用如何导致安全发布在下面的代码中.有人能给出一个易于理解的解释.
public class SafeListener
{
private final EventListener listener;
private SafeListener()
{
listener = new EventListener()
{ public void onEvent(Event e)
{ doSomething(e); }
};
}
public static SafeListener newInstance(EventSource source)
{
SafeListener safe = new SafeListener();
source.registerListener(safe.listener);
return safe;
}
}
Run Code Online (Sandbox Code Playgroud) 我可以使用标准的Collections类(而不是并发的类),只要我确保代码不会在多个线程上进行数据更改.我正在谈论的代码完全在我的控制之下,并且在初始(单线程)填充阶段之后我不会改变它.
我知道一些类如DateFormat不是线程安全的,因为它们在使用时存储中间状态.集合(ArrayList,Tree Map等)是否安全?
我遇到了一个奇怪的场景,当我们初始化一个新的对象并且对象数量是JVm非常高时,是否有可能JVM重新使用已经创建的对象?
abc a = new abc();
a.setAttribute("aaaa");
.........
a...is no longer being used...and has not yet been garbage collected by the JVM. There are multiple threads creating 5000 instances of class abc..
again, abc a = new abc();
Sysout(a.getAttribute()); // This prints "aaaa" set for an earlier instance!
Run Code Online (Sandbox Code Playgroud)
是否有可能重新使用实例.?有没有人遇到过这种情况?
已经有类似的问题,但它没有回答以下问题。众所周知,字段的值不一定在线程之间立即同步。但是局部变量也是这种情况吗?可以抛出 IllegalStateException 吗?
public static void main(String[] args) {
final Thread mainThread = Thread.currentThread();
final Integer[] shared = new Integer[1];
new Thread(new Runnable() {
@Override
public void run() {
shared[0] = 1;
mainThread.interrupt();
}
}).start();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
if (shared[0] == null) throw new IllegalStateException("Is this possible?");
}
}
Run Code Online (Sandbox Code Playgroud) java ×7
collections ×1
concurrency ×1
creation ×1
javascript ×1
jvm ×1
locking ×1
nashorn ×1
object ×1
servlets ×1