0

I have a c# code, which must be ported to java. And now I encounter this instruction mentioned in my topic.

this.SynchronizingObject.Invoke((MethodInvoker)delegate()
                {
                    // do my stuff
                }, null);

Now I must ask in here, what can I do to implement this in java. Is there any synchronizing object in java ?

icbytes
  • 1,831
  • 1
  • 17
  • 27
  • There is no synchronizing object as such but we can synchronize ant part of code with any Object using synchronized keyword. – Aniket Thakur Dec 13 '13 at 12:28
  • What type is `this.SynchronizingObject`, and what does its `Invoke` method do? I assume it invokes the provided delegate on some specific thread, I assume, but the answer depends on the specifics. – gustafc Dec 13 '13 at 12:38
  • It is simply a class-object implementing an own interface. The invoked methods basically do a http web request and process the received data. – icbytes Dec 13 '13 at 13:24

2 Answers2

0

do like this

SynchronizingObject synchronizingObject = new SynchronizingObject();    
synchronized (synchronizingObject ) {
   change your synchronizingObject  here

}

or

synchronized  void changeSynchronizingObject(SynchronizingObject synchronizingObject){
        change your synchronizingObject  here
} 
Prabhakaran Ramaswamy
  • 25,706
  • 10
  • 57
  • 64
0

First you should learn about synchronized keyword and synchronized blocks if you are from C# background. It is just a mutex you studied in your OS lessons. Now in order to achieve you can use the below code segment.

Type 1

AnyObject anyObject = new AnyObject();
 synchronized(anyObject) {
   // your critical section code here
 }

Type 2

synchronized <return_type> function() {
  //critical section 
}

This method takes the instance of class in which this method is implemented as a lock.

This should be of some help.

sakthisundar
  • 3,278
  • 3
  • 16
  • 29
  • Thx so far. I already read about the synchronized keyword. But only as a recommended equivalent to the lock(this) {} stub of c#. – icbytes Dec 13 '13 at 13:26