Questions tagged [synchronized]

A block or method is said to be 'synchronized' if entry to it is controlled by the Java `synchronized` keyword. This causes access to it to be restricted to a single thread at a time: when concurrent accesses occur, they are sequentialized in an unspecified order.

A block or method is said to be 'synchronized' if entry to it is controlled by the Java synchronized keyword. This causes access to it to be restricted to a single thread at a time: when concurrent accesses occur, they are sequentialized in an unspecified order.

1865 questions
0
votes
1 answer

Asking about threading, arrays and cache memory

I hope in a good manner :-) I wrote this piece of code. What I wished to do, is to build something like "cache". I assumed that I had to watch for different threads, as might many calls get to that class, so I tried the ThreadLocal…
hephestos
  • 434
  • 1
  • 6
  • 19
0
votes
1 answer

Why code follow synchronization?

I am learning java thread and i wrote below code and try to run this code. i am not using any synchronize concept but output is in consistent format. Please help me to figure out the actual reason. package check; import…
0
votes
1 answer

Java Synchronization (for specific example)

package sync.block.lock.on.thisObj; public class Counter { int count = 0; public void increment() { synchronized (this) { count++; // STATEMENT 1 //System.out.println(count); } …
0
votes
1 answer

How to refresh/Synch View based on a Query with dynamic range?

I have a simple View with a Query , my custom Query has a Dynamically Range. In class SysQueryRangeUtil I have insered a my custom public static method and copy it in my Custom Query. This range, work well. But I have a problem, when the range…
ulisses
  • 1,549
  • 3
  • 37
  • 85
0
votes
1 answer

CopyOnWriteArraylist synchronized methods

Its method from CopyOnWriteArrayList.class public synchronized boolean set(E e) { Object[] newElements = elements.clone(); @SuppressWarnings("unchecked") E result = (E) newElements[index]; newElements[index] = e; …
Sky
  • 521
  • 1
  • 5
  • 14
0
votes
1 answer

Alternatives to pervasive use of the @synchronized directive on mutable array access

I am designing a singleton class in Objective-C which will be accessed by multiple threads. There are 3-4 NSMutableArrays in my class, outside classes have access with read, add and remove operations which is of course wrapped in this class. As…
0
votes
1 answer

Multi-threading in Java: field not incrementing as expected

I'm having trouble to create one instance and make it shared between all threads, here's my code: Here's main method: public static void main(String... args) throws IOException, ClassNotFoundException { MainApp mainApp = new MainApp(); …
Fisher Coder
  • 3,278
  • 12
  • 49
  • 84
0
votes
1 answer

Synchronised Map in onStartCommand Service

I am trying to avoid executing duplicate tasks on a Service by using a synchronised Map in the onStartMethod and then checking that a key is not already stored. However, so far is not working, it is executing the same thing twice if I call start the…
Julio
  • 283
  • 2
  • 13
0
votes
1 answer

Is flushing automatic in TextWriter Synchronized?

I checked this code in debug. The file exists when it's executing, and string contents has some text. TextWriter.Synchronized(new StreamWriter(tmpOutput)).WriteLine(contents) Yet the file is empty after this line is executing. Is Flush…
Jeanne Lane
  • 495
  • 1
  • 9
  • 26
0
votes
3 answers

How Class level Synchronization implement in Java?

I am having an object of a class having some state. This object has two methods (method1() and method2()), both are changing the state of obj. method1() is synchronized but method2() is not synchronized. Now two threads, thread1 and threads2…
Vimal Panchal
  • 301
  • 1
  • 5
  • 15
0
votes
1 answer

Java multithreading. synchronized(this) on a Thread class

I've stumbled upon this interesting tutorial on multiple client-server chat model. I understood everything except for one thing. In the implementation of class MultiThreadChatServerSync, he explains that we need to synchronize parts of the code…
Patrick
  • 191
  • 2
  • 8
0
votes
1 answer

Thread Sleep method behaviour in synchronized block

public class DeadlockDemo2 { public static Object Lock1 = new Object(); public static Object Lock2 = new Object(); public static void main(String[] args) { // TODO Auto-generated method stub ThreadDemo1 demo1 = new…
0
votes
1 answer

improper output of synchronization in java

In the code, the thread output is not properly synchronized. The output should be the numbers in increasing order. here is the code public class Prog { public static void main(String[] args) { Thread a = new Thread(new Writer(), "A"); …
Doc
  • 10,831
  • 3
  • 39
  • 63
0
votes
5 answers

Why do we need to specify the lock for synchronized statements?

Given that there's only one lock for each instance of a class, then why doesn't Java just allow us to do this: void method() { synchronized { // do something } // do other things } instead of this: void method() { …
Sid Go
  • 2,041
  • 3
  • 16
  • 29
0
votes
1 answer

JPanels not showing up, kinda

I am an in-school amateur making a snake game in java, it is not complete and i have run into a problem that i cant seem to fix. the first and second parts of the body show up just fine after eating the target, but after that they do not. code that…