Golgappa.net | Golgappa.org | BagIndia.net | BodyIndia.Com | CabIndia.net | CarsBikes.net | CarsBikes.org | CashIndia.net | ConsumerIndia.net | CookingIndia.net | DataIndia.net | DealIndia.net | EmailIndia.net | FirstTablet.com | FirstTourist.com | ForsaleIndia.net | IndiaBody.Com | IndiaCab.net | IndiaCash.net | IndiaModel.net | KidForum.net | OfficeIndia.net | PaysIndia.com | RestaurantIndia.net | RestaurantsIndia.net | SaleForum.net | SellForum.net | SoldIndia.com | StarIndia.net | TomatoCab.com | TomatoCabs.com | TownIndia.com
Interested to Buy Any Domain ? << Click Here >> for more details...


This is related to threads. I have a class with synchronized
method m1(). Can I create different instances of this class
and execute the m1() for different threads?

Answers were Sorted based on User's Feedback



This is related to threads. I have a class with synchronized method m1(). Can I create different i..

Answer / shashidhar

When a thread enters the Synchronized method the object on
this the method is called gets locked so no other thread
cannot cal that or any other method on same object.

Is This Answer Correct ?    4 Yes 1 No

This is related to threads. I have a class with synchronized method m1(). Can I create different i..

Answer / qim2010

Putting code inside a synchronized block makes the compiler
append instructions to acquire the lock on the specified
object before executing the code, and release it afterwards
(either because the code finishes normally or abnormally).
Between acquiring the lock and releasing it, a thread is
said to "own" the lock. At the point of Thread A wanting to
acquire the lock, if Thread B already owns the it, then
Thread A must wait for Thread B to release it. Thus in the
example below, simultaneous calls to increment() and
getCount() will always behave as expected; a read could not
"step in" while another thread was in the middle of
incrementing.

public class Counter {
private int count = 0;
public void increment() {
synchronized (this) {
count++;
}
}
public int getCount() {
synchronized (this) {
return count;
}
}
}

Is This Answer Correct ?    0 Yes 0 No

This is related to threads. I have a class with synchronized method m1(). Can I create different i..

Answer / haneef

see by the synchronized it won't allow the other thread at a
time, but after completion of 1st thread it allows, so you
can create different instances.

see the following code

package app;

class C1 extends Thread {
public void run() {
m1();
}

public synchronized void m1() {
System.out.println("m1()");
}
}

public class Main {
public static void main(String[] args) {

C1 c1 = new C1();
C1 c2 = new C1();
C1 c3 = new C1();

Thread t1 = new Thread(c1);
Thread t2 = new Thread(c2);
Thread t3 = new Thread(c3);

t1.start();
t2.start();
t3.start();

}
}

Is This Answer Correct ?    0 Yes 1 No

Post New Answer

More Core Java Interview Questions

What is compareto?

0 Answers  


why are wait(), notify() and notifyall() methods defined in the object class? : Java thread

0 Answers  


What are order of precedence and associativity, and how are they used?

1 Answers  


Can you explain inner class.

0 Answers  


What is output buffer?

0 Answers  


What is difference between string and new string?

0 Answers  


write a program to create an vector and listeterator.and value should be enter through keyboard.

1 Answers   Axcend,


What is diamond operator in java?

0 Answers  


Convert a binary search tree to a sorted doubly linked list inplace.

1 Answers   Amazon,


Can we extend a class with private constructor?

0 Answers  


Can we rethrow the same exception from catch handler?

0 Answers  


What is a buffer in computer?

0 Answers  


Categories