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
Difference between string, string builder, and string buffer?
What is type conversion in java?
How to sort an array from smallest to largest java?
What is an empty class? What functionality does it offer in Java?
What are the differences between getting and load method?
What advantage do java's layout managers provide over traditional windowing systems?
What kind of variables a class can consist of?
What is the console in java?
when a request is generated from apache tomcat 5.5 and goes to oracle 10g or mysql,,, how the oracle or mysql reads the request as apache is a web server and oracle 10g is application server? when the oracle 10g provides response, how the apche tomcat reads it???
What is method overloading in java ?
What is a war file?
How do you escape a string?
Java is pass by value or pass by reference? Explain
Can inner class be public in java?
What is ++ a in java?