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


Please Help Members By Posting Answers For Below Questions

Can you make a constructor final in Java?

624


Why destructor is not used in java?

518


What classes of exceptions may be thrown by a throw statement?

524


How to sort array of 0 and 1 in java?

541


What is class forname used for?

538






How can you read content from file in java?

597


What is difference between static variable and global variable?

546


Is set ordered in java?

570


What is boolean logic?

559


Explain constructors and types of constructors in java.

642


What is final method in java?

539


What is the difference between a window and a frame in java programming?

589


Define "Access specifiers" in java.

608


What is java and their uses?

547


Explain the scope of a variable.

627