java-threads

Resolving Java thread visibility and concurrency error using Map compute

本小妞迷上赌 提交于 2020-04-16 02:54:29
问题 I use Java 8. I have an event handler that accepts events with a high rate, (n per second) and I want to flush them out to storage when I get so many of them (in this simplified example 1000) Do I have a visibility error on line 25 myCache.get(event.getKey()).add(event.getBean()); ? Should I synchronize on handleEvent() method? public class myClass extends MySimpleEventHanlder { private Map<String, List<MyBean>> myCache; private ScheduledExecutorService scheduler; public void MyClass() {

Parsing @Context UriInfo to java thread

谁说我不能喝 提交于 2020-01-11 07:49:06
问题 I'm trying to parse @Context UriInfo to another thread and do some task. But when i try to run it, it gives the error as Exception in thread "Thread-691" org.jboss.resteasy.spi.LoggableFailure: Unable to find contextual data of type: javax.ws.rs.core.UriInfo My code as follows @GET @Path("/thread") public void thread(@Context UriInfo url){ Runnable run = new Runnable() { @Override public void run() { System.out.println(">>>>>>>>>>>> " + url.getRequestUri().getQuery()); } }; Thread t = new

How can I interrupt a thread created from within a method?

醉酒当歌 提交于 2020-01-06 20:23:56
问题 I know that you can interrupt a thread created from say a runnable class, but how can I interrupt this thread I created from a method? Using a volatile boolean isn't working for me, so I assume either there is a better way to do this, or I need to somehow interrupt it. I don't want to interrupt all threads, just this one. I created a method that kicks off a Thread like this: public static void StartSyncThread() { new Thread() { public void run() { isRunning = true; // Set to true so while

Is there a stack space for every thread?

坚强是说给别人听的谎言 提交于 2020-01-02 01:13:10
问题 If I understand correctly the stack is for local primities and references to the objects in the heap. So what happens if you have more than one threads? Do they share the same stack space at the same time (but different areas) or does the JRE switch contexts and load-deload the stack content when switching between threads? Or does the JRE allocate separate stacks for each threads? 回答1: Or does the JRE allocate separate stacks for each threads? Conceptually yes. (See this JVM spec link, for

How to notify a specific thread in Java

孤者浪人 提交于 2019-12-29 08:55:11
问题 How I can call a particular thread in inter-thread communication? In the program below I have two threads t1 and t2 . When I call t1.notify() it raises: Exception in thread "Thread-1" java.lang.IllegalMonitorStateException at java.lang.Object.notify(Native Method) at Shared.methodTwo(NotifyThread.java:43) at Thread2.run(NotifyThread.java:77) Error class Shared { Thread1 t1 ; Thread2 t2 ; void ThreadInit( Thread1 t1 , Thread2 t2 ) { this.t1 = t1 ; this.t2 = t2 ; } synchronized void methodOne()

Why Thread.sleep is bad to use

帅比萌擦擦* 提交于 2019-12-28 05:15:29
问题 Apologies for this repeated question but I haven't found any satisfactory answers yet. Most of the question had their own specific use case: Java - alternative to thread.sleep Is there any better or alternative way to skip/avoid using Thread.sleep(1000) in Java? My question is for the very generic use case. Wait for a condition to complete. Do some operation. Check for a condition. If the condition is not true, wait for some time and again do the same operation. For e.g. Consider a method

Vaadin 8: How to show ProgressBar window before navigating to other view with long loading

荒凉一梦 提交于 2019-12-24 20:59:22
问题 I want to navigate to a view that displays large data in a grid after a button click. I know there is lazy loading but I want to load all data to be able to sort by clicking the header. With lazy loading I could only sort one column. Button viewDataBtn = new Button("View scan data"); viewDataBtn .addClickListener(e -> { UI.getCurrent().getNavigator().navigateTo("scandataview/" + name); }); This does work but there is a long break until the new view is visible. Therefore, I want to show a

Java - Threads state when performing I/O operations

谁说胖子不能爱 提交于 2019-12-23 23:22:59
问题 Suppose a Java thread performs some I/O operation like reading a file with traditional blocking Java I/O. The question is: What is the state of the thread while waiting? I don't know if it is RUNNING (doing some active wait) or WAITING (maybe there is some kind of monitor that wakes up the thread when file data is ready). How can I find it out? Thanks. 回答1: A thread that is blocked in an I/O syscall should be in RUNNABLE state, according to my reading of the javadocs. public static final

Print odd and even using two threads in Java

▼魔方 西西 提交于 2019-12-23 04:09:04
问题 I am trying to do it using two threads like below. Can someone point the obvious mistake I am doing here? public class OddEven { public static boolean available = false; public static Queue<Integer> queue = new LinkedList<Integer>(); static Thread threadEven = new Thread() { @Override public void run() { printEven(); } public synchronized void printEven() { while (!available) { try { wait(); Thread.sleep(2000); } catch (InterruptedException e) { } } System.out.println(queue.remove());

What is the order of execution of newly created threads in java

↘锁芯ラ 提交于 2019-12-19 21:19:49
问题 class Test { boolean isFirstThread = true; private synchronized void printer(int threadNo) { if(isFirstThread) { try { Thread.sleep(2000); } catch (InterruptedException e) { e.printStackTrace(); } } isFirstThread = false; System.out.println(threadNo); } public void starter() { new Thread(){ @Override() public void run() { printer(0); } }.start(); new Thread(){ @Override() public void run() { printer(1); } }.start(); new Thread(){ @Override() public void run() { printer(2); } }.start(); new