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?
Answer Posted / 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 View All Answers
Is ++ operator thread-safe in java?
What does flag mean in java?
what are the high-level thread states? : Java thread
Can we overload destructor in java?
What is meant by singleton class?
What is flush () in java?
How do you remove duplicates from an array in java?
What are java packages? What's the significance of packages?
Is empty string in java?
What are the advantages of arraylist over arrays?
What is the purpose of main function in java?
What is the difference between a local variable and an instance variable?
What is thread start?
When do you get classcastexception?
What is the platform?