16.6 You is given a class with synchronized method A and a normal method B. If you have both threads in one instance for a program, can they both execute a at the same time? Can They execute A and B at the same time?
When we add the Synchronized keyword to a method, we ensure that two threads cannot execute the same object at the same time. So the answer to the first question is based on the situation, if two threads have the same instance of the object, then the answer is no, they can't execute method A at the same time, but when they have different instances of the object, they can. We can use the concept of lock to understand that a synchronized method is a method of locking on the instance object, which prevents other threads from executing other synchronized methods of the instance.
For the second part, because method B does not have the Synchronized keyword, when thread 2 runs method B There is nothing that prevents thread 1 from running method A, and whether threads 1 and 2 have the same instance object. In summary, it is worth remembering that only one synchronous synchronized method can be executed in an instance of an object, other threads can execute the instance's Unsynchronized non-synchronized method, or they can execute any method of the other instance of the object.
Public classFoo {PrivateString name; PublicFoo (String nm) {name=nm; } PublicString GetName () {returnname; } Public voidpause () {Try{Thread.Sleep (1000 * 3); } Catch(interruptedexception e) {e.printstacktrace (); } } Public synchronized voidMethodA (String threadname) {System.out.println ("Thread" + ThreadName + "starting;" + name + ". MethodA ()"); Pause (); System.out.println ("Thread" + ThreadName + "ending:" + name + ". MethodA ()"); } Public voidMethodB (String threadname) {System.out.println ("Thread" + ThreadName + "starting:" + name + ". MethodB ()"); Pause (); System.out.println ("Thread" + ThreadName + "ending:" + name + ". MethodB ()"); }} Public classMyThreadextendsThread {Privatefoo foo; PublicString name; PublicString FirstMethod; PublicMyThread (foo F, string nm, String FM) {foo=F; Name=nm; FirstMethod=FM; } Public voidrun () {if(Firstmethod.equals ("A") {Foo.methoda (name); } Else{foo.methodb (name); } }} Public classJ { Public Static voidMain (string[] args) {System.out.println ("Part 1 Demo with same instance."); Foo Fooa=NewFoo ("Objectone"); MyThread thread1a=NewMyThread (Fooa, "Dog", "A"); MyThread thread2a=NewMyThread (Fooa, "Cat", "A"); Thread1a.start (); Thread2a.start (); while(Thread1a.isalive () | |thread2a.isalive ()) {} System.out.println ("\ n"); System.out.println ("Part 1 Demo with different instance."); Foo fooB1=NewFoo ("Objectone"); Foo fooB2=NewFoo ("Obejcttwo"); MyThread thread1b=NewMyThread (fooB1, "Dog", "A"); MyThread thread2b=NewMyThread (fooB1, "Cat", "A"); Thread1b.start (); Thread2b.start (); while(Thread1b.isalive () | |thread2b.isalive ()) {}; System.out.println ("\ n"); System.out.println ("Part 2 Demo."); Foo FOOC=NewFoo ("Objectone"); MyThread thread1c=NewMyThread (FOOC, "Dog", "A"); MyThread thread2c=NewMyThread (FOOC, "Cat", "B"); Thread1c.start (); Thread2c.start (); }}
Careercup all in one topic summary
[Careercup] 16.6 Synchronized method synchronization methods