Skip to main content

All Questions

Filter by
Sorted by
Tagged with
-1 votes
1 answer
107 views

Java synchronization not working properly

I am trying to implement a custom HashMap for multithreading environment. Why is this giving inconsistent results? I think multiple threads are accessing the put method simultaneously. class Main{ ...
A7781's user avatar
  • 1
-1 votes
1 answer
336 views

Print numbers from one to ten

I have to print the numbers between one to ten , with ten threads and they got to be consecutive numbers. I mean Thread0 - 10 print 1,1,1,1,1,1,1,1,1,1 then Thread 0-10 print 2,2,2,2,2,2,2,2,,2,2 ...
Slavi Donchev's user avatar
0 votes
1 answer
39 views

Synchronized getter and synchronized thread run method in java

The followiing code uses threads to calculate the max value in a subarry, and then calculates the max value out of the max values the threads returned. I have a bug that the main thread doesn't wait ...
GuyO's user avatar
  • 51
1 vote
1 answer
93 views

How does Object.notify() work with Object.wait()?

I am trying to track a resource which behaves in an asymmetric manner. That is, it responds immediately to a start() request, but finishes processing a cancel() request at a significant delay. For ...
WebViewer's user avatar
  • 831
0 votes
1 answer
59 views

Where does the program know the exact value of the instance variable if I don't use synchronized in Java?

I'm learning about Java multithreading and I'm doing some simple tests with synchronized blocks and without synchronized block. public class MultipleMonitorObjects { private Object monitor1 = new ...
elvis's user avatar
  • 1,238
1 vote
1 answer
216 views

How to check for changes in a List in java?

I have a question, I want to know if it's possible to check continuously for adds to a list. I have a node with a file to transfer, I connect it to a directory. I have another node without the file ...
Coder101's user avatar
1 vote
1 answer
507 views

My java unit test failed if there is a call to wait method inside a synchronized method

I am learning multi-threads programming in java recently. And I don't understand why the following test case will fail. Any explanation will be much appreciated. Here is MyCounter.java. public class ...
Chris's user avatar
  • 13
0 votes
1 answer
145 views

Does Java also guarantee that all variable changes before synchronized will be visible to the next thread which synchronizes on same object?

In the below code will Java ensure that latest copy of a is visible to the thread which calls getAB()? I understand that the values returned by getAB() might not be the same as set by setAB, but will ...
Piyush's user avatar
  • 638
2 votes
2 answers
151 views

Java multithreading :Start 2 threads, print 1-100 by turns. result like this: but it is not running [duplicate]

Two threads needs to print in this order- Thread1:0 Thread2:::0 Thread1:1 Thread2:::1 Thread1:2 Thread2:::2 Thread1:3 Thread2:::3 Thread1:::4 Thread2:::4 . . ...
Hao Zhang's user avatar
0 votes
2 answers
168 views

Need code to understand synchronized static and non-static methods being accessed at the same time by two different threads on the same instance

class A { synchronized static void m1() { System.out.println("In m1 A"); } synchronized void m2() { System.out.println("In m2 A"); } } ...
crimson_joey's user avatar
4 votes
1 answer
796 views

Creating an object from string and using as monitor in synchronized block [duplicate]

I am using an annotation on some classes and by reflection I get some string of that annotation which is unique for every class. I am thinking of using that string to synchronize a block of code, but ...
aakash singh's user avatar
0 votes
1 answer
746 views

sleep() and context switching in java threads

Lets assume a situation like this : Lets say Thread0 access the lockObject first and then Thread0 going to sleep for 1000ms. synchronized(lockObject) { Thread0.sleep(1000); } Thread1 also ...
kenWay's user avatar
  • 101
1 vote
1 answer
706 views

understanding synchronizedlist in java

I having an ArrayList which is modified by one thread and read by another. Reading thread is happy to read whatever is left in the list after modification. It should stop when all the items are ...
Renjith's user avatar
  • 1,154
0 votes
1 answer
464 views

static method with a synchronized block inside, blocks the entire class in java?

I have a class that executes 3 threads, and on every thread I ask and check on the global variables. I´m trying to lock each variable separetly, I dont´t want to block the entire class every time I ...
daniel gon's user avatar
0 votes
2 answers
1k views

How to make a circular queue completely thread safe

At the minute I have just added synchronized to the majority of methods as it appears that without it, these methods with not be thread safe. Is there anything else that I need to implement to ensure ...
user3704648's user avatar

15 30 50 per page