subject :

Judge the following 8 In this case , Output content

Topic 1 : One Number Instance object number, Two non static synchronization methods getOne,getTwo, Two thread printouts ( One thread call number.getOne, Another thread calls number.getTwo), Judge output

package com.dx.juc.test;
public class Thread8Monitor {
public static void main(String[] args) {
final Number number=new Number(); new Thread(new Runnable() {
public void run() {
number.getOne();
}
}).start(); new Thread(new Runnable() {
public void run() {
number.getTwo();
}
}).start();
}
} class Number{
public synchronized void getOne(){
System.out.println("one");
} public synchronized void getTwo(){
System.out.println("two");
}
}

Output results :

one
two

Topic two : One Number Instance object number, Two non static synchronization methods getOne,getTwo, to getOne add to Thread.sleep(3000), Two thread printouts ( One thread call number.getOne, Another thread calls number.getTwo), Judge output

package com.dx.juc.test;
public class Thread8Monitor {
public static void main(String[] args) {
final Number number=new Number(); new Thread(new Runnable() {
public void run() {
number.getOne();
}
}).start(); new Thread(new Runnable() {
public void run() {
number.getTwo();
}
}).start();
}
} class Number{
public synchronized void getOne(){
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("one");
} public synchronized void getTwo(){
System.out.println("two");
}
}

Output results :

one
two

Topic three : One Number Instance object number, Two non static synchronization methods getOne,getTwo, to getOne add to Thread.sleep(3000), New common method getThree Method , Three thread printouts ( One thread call number.getOne, Another thread calls number.getTwo, One thread call number.getThree), Judge output

package com.dx.juc.test;
public class Thread8Monitor {
public static void main(String[] args) {
final Number number=new Number(); new Thread(new Runnable() {
public void run() {
number.getOne();
}
}).start(); new Thread(new Runnable() {
public void run() {
number.getTwo();
}
}).start(); new Thread(new Runnable() {
public void run() {
number.getThree();
}
}).start();
}
} class Number{
public synchronized void getOne(){
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("one");
} public synchronized void getTwo(){
System.out.println("two");
}
public void getThree(){
System.out.println("three");
}
}

Output results :

three
one
two

Topic four : Two Number Instance object number,number2, Two non static synchronization methods getOne,getTwo, to getOne add to Thread.sleep(3000), Two thread printouts ( One thread call number.getOne, Another thread calls number2.getTwo), Judge output

package com.dx.juc.test;
public class Thread8Monitor {
public static void main(String[] args) {
final Number number=new Number();
final Number number2=new Number(); new Thread(new Runnable() {
public void run() {
number.getOne();
}
}).start(); new Thread(new Runnable() {
public void run() {
// number.getTwo();
number2.getTwo();
}
}).start();
}
} class Number{
public synchronized void getOne(){
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("one");
} public synchronized void getTwo(){
System.out.println("two");
}
}

Output results :

two
one

Topic 5 : One Number Instance object number, A static synchronization method getOne, A non static synchronization method getTwo, to getOne add to Thread.sleep(3000), Two thread printouts ( One thread call number.getOne, Another thread calls number.getTwo), Judge output

package com.dx.juc.test;
public class Thread8Monitor {
public static void main(String[] args) {
final Number number=new Number();new Thread(new Runnable() {
public void run() {
number.getOne();
}
}).start(); new Thread(new Runnable() {
public void run() {
number.getTwo();
}
}).start();
}
} class Number{
public static synchronized void getOne(){
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("one");
} public synchronized void getTwo(){
System.out.println("two");
}
}

Output results :

two
one

Topic 6 : One Number Instance object number, Two static synchronization methods getOne,getTwo, to getOne add to Thread.sleep(3000), Two thread printouts ( One thread call number.getOne, Another thread calls number.getTwo), Judge output

package com.dx.juc.test;
public class Thread8Monitor {
public static void main(String[] args) {
final Number number=new Number();
//final Number number2=new Number(); new Thread(new Runnable() {
public void run() {
number.getOne();
}
}).start(); new Thread(new Runnable() {
public void run() {
number.getTwo();
// number2.getTwo();
}
}).start();
}
} class Number{
public static synchronized void getOne(){
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("one");
} public static synchronized void getTwo(){
System.out.println("two");
}
}

Output results :

one
two

Topic 7 : Two Number Instance object number,number2, A static synchronization method getOne, A non static synchronization method getTwo, to getOne add to Thread.sleep(3000), Two thread printouts ( One thread call number.getOne, Another thread calls number.2getTwo), Judge output

package com.dx.juc.test;
public class Thread8Monitor {
public static void main(String[] args) {
final Number number=new Number();
final Number number2=new Number(); new Thread(new Runnable() {
public void run() {
number.getOne();
}
}).start(); new Thread(new Runnable() {
public void run() {
// number.getTwo();
number2.getTwo();
}
}).start();
}
} class Number{
public static synchronized void getOne(){
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("one");
} public synchronized void getTwo(){
System.out.println("two");
}
}

Output results :

two
one

Topic 8 : Two Number Instance object number,number2, Two static synchronization methods getOne,getTwo, to getOne add to Thread.sleep(3000), Two thread printouts ( One thread call number.getOne, Another thread calls number.2getTwo), Judge output

package com.dx.juc.test;
public class Thread8Monitor {
public static void main(String[] args) {
final Number number=new Number();
final Number number2=new Number(); new Thread(new Runnable() {
public void run() {
number.getOne();
}
}).start(); new Thread(new Runnable() {
public void run() {
// number.getTwo();
number2.getTwo();
}
}).start();
}
} class Number{
public static synchronized void getOne(){
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("one");
} public static synchronized void getTwo(){
System.out.println("two");
}
}

Output results :

one
two

summary :

1) Static method locks are locked Number.class( Class bytecode , It's also Class Example ); No matter how many static methods there are in a class , No matter how many threads are accessing them , These threads share the same lock .

2) Non static method locks are locked this, That is, the current instance object .this The instance object of the current class , A lock is common to the same instance object , Different instance objects are used without lock .

Java-JUC( 11、 ... and ): Threads 8 More about locks

  1. JAVA language norm - Synchronization of thread and lock chapters 、 Waiting and notifying

    JAVA language norm : Threads and locks 1 Sync java Programming languages provide multiple mechanisms for communication between threads . The most basic of these methods is synchronization , This method is implemented using a monitor .JAVA Each object in is associated with a monitor , A thread can lock and unlock monitoring ...

  2. Java - "JUC" ReentrantLock Get the lock

    [Java Concurrent programming practice ]-----“J.U.C”:ReentrantLock One introduction ReentrantLock Introduce ReentrantLock It's a reentrant mutex , Also known as “ An exclusive lock ”. seeing the name of a thing one thinks of its function ,R ...

  3. Java - "JUC" ReentrantLock Release the lock

    Java Multithreading series --“JUC lock ”04 And Fair lock ( Two ) Release the fairness lock ( be based on JDK1.7.0_40) 1. unlock() unlock() stay ReentrantLock.java Implemented in , Source code is as follows : p ...

  4. JUC—— Thread synchronization lock ( Introduction to lock handling mechanism )

    Introduction to lock handling mechanism juc The core of the framework is concurrent access and data security operation , When concurrent access is performed, if the lock is not properly controlled , It can cause blocking problems like deadlock . In order to solve this defect ,juc It focuses on the concept of lock again ...

  5. java High concurrency series - The first 12 God JUC:ReentrantLock Reentrant lock

    java High concurrency series - The first 12 God JUC:ReentrantLock Reentrant lock This article begins with juc Some classes commonly used in , It's estimated that there will be more than ten articles . synchronized The limitations of synchronized yes java built-in ...

  6. java Threads Lock Lock use Condition Implementation of thread waiting (await) And notice (signal)

    One .Condition class In the front, we learn from synchronized Thread waiting for lock coordination (Object.wait) With thread notification (Object.notify), So for JDK1.5 Of  java.util.c ...

  7. Java Threads : lock

    One . Principle of lock Java Each object in has a built-in lock , When the program runs to non static synchronized Synchronization method , Automatically get the current instance of the code class being executed (this example ) About the lock . Getting the lock of an object is also called getting the lock . lock ...

  8. Java - “JUC” lock

    [Java Concurrent programming practice ]-----“J.U.C”: lock ,lock   stay java There are two ways to implement the locking mechanism in , One is in the previous blog ([java7 Concurrent programming practice ]----- Thread synchronization mechanism :synchronized) ...

  9. Java Threads and locks

    Synchronization synchronized Syntax can get locks , When other threads hold the lock, the thread will enter the waiting state if it wants to acquire the lock , Until no other thread holds the lock Display and use synchronized (lo ...

  10. Java Threads and locks

    Summary : Thread implementation method . Thread scheduling . Thread state and transition . Thread safety (5 Kinds of classification .3 There are ways to do it . Lock Optimization Technology ) The process is OS Basic unit for resource allocation , Thread is CPU The basic unit of dispatch . 1. Thread implementation method Please see the I'm a progressive ...

Random recommendation

  1. webstorm Basic use summary

    webstorm Basic use summary ( It's not just a bunch of shortcuts ) notes : Link to the original text :  Waterwheel :  The content of this time is that I write that when I think of that , I usually write code when some habits , Not necessarily the best , Communication is the key ! 1:  shift+e ...

  2. Reverse Linked list Recursive implementation

    #include<iostream> struct node{ int payload; node* next; }; void bianli(node* head){ node* ite ...

  3. WebApi 2: Attribute routing [Route()],attribute routing

    original text :http://www.asp.net/web-api/overview/web-api-routing-and-actions/attribute-routing-in-web-api-2 attribute ...

  4. Extraterrestrial virtual hosts span web Directory file read vulnerability

    Off board virtual host cross directory read file vulnerability , It needs certain conditions . The problem is in the following file , These files do not have strict execution permissions , Current IIS Users can use them to execute commands smoothly : c:\windows\7i24IISLOG.exe ...

  5. [ skill ] hold Excel How to insert the data in the database

    . If first in 6 The last column of row data inserts a column of data , Please write down the names first , Then enter the number under this column in the first row , Then select the cell and drag it down to see the small black box , Double click the black dot in the lower right corner ,6 Row data is Will fill in the default number ...

  6. Windows Azure Mobile terminal cloud service management ( Public beta )

    overview The clouds are far away , Management is in your hands . On this move first Cloud first world , Services are no longer provided on a request basis , It's the mode of coming to your side for your convenience . We will launch several mobile devices to help you manage your cloud services anytime, anywhere . Among the first batch of sharp tools ...

  7. jquery Of change event

    jquery Of change event . <script src="jquery.min.js" type="text/javascript">< ...

  8. The first 1 Chapter Git The way of version control

    Version control system (Version Control System,VCS) It can help us record and track the changes of the contents of the files in the project . 1.1 Version Library Version Library (Repository) Version control system is used to store all historical data ...

  9. Solve the whole station ie6 Next PNG The image opacity problem only needs a few lines of code

    Solve the whole station ie6 Next PNG The picture is opaque. Just copy the following lines of code and paste them at the bottom of your document , The package you need to use DD_belatedPNG_0.0.8a.js Download it on the Internet The code starts /* Add the following code at the bottom of the document */ &l ...

  10. android How to open a program in the system AccessibilityService

    android How to open a program in the system AccessibilityService( System support services )? The usual practice is to register AccessibilityService( ancillary services ) Then jump to the setup startup service page to guide the user ...