Thread mutex in Java

Source: Internet
Author: User

The following code is available:

public class TraditionalThreadSynchronized {public static void main(String[] args) {new TraditionalThreadSynchronized().init();}public void init() {final Outputer outputer = new Outputer();new Thread(new Runnable() {@Overridepublic void run() {while (true) {try {Thread.sleep(100);} catch (InterruptedException e) {e.printStackTrace();}outputer.out("bbbbbbbbb");}}}).start();new Thread(new Runnable() {@Overridepublic void run() {while (true) {try {Thread.sleep(100);} catch (InterruptedException e) {e.printStackTrace();}outputer.out("aaa");}}}).start();}class Outputer {public synchronized void out(String name) {int len = name.length();for (int i = 0; i < len; i++) {System.out.print(name.charAt(i));}System.out.println();}public void out1(String name) {int len = name.length();synchronized (Outputer.class) {for (int i = 0; i < len; i++) {System.out.print(name.charAt(i));}System.out.println();}}}}

Of course, the out and out1 methods can be mutually exclusive,

However, in the following code

public class TraditionalThreadSynchronized {public static void main(String[] args) {new TraditionalThreadSynchronized().init();}public void init() {final Outputer outputer = new Outputer();new Thread(new Runnable() {@Overridepublic void run() {while (true) {try {Thread.sleep(100);} catch (InterruptedException e) {e.printStackTrace();}outputer.out("bbbbbbbbb");}}}).start();new Thread(new Runnable() {@Overridepublic void run() {while (true) {try {Thread.sleep(100);} catch (InterruptedException e) {e.printStackTrace();}outputer.out3("aaa");}}}).start();}static class Outputer {public synchronized void out(String name) {int len = name.length();for (int i = 0; i < len; i++) {System.out.print(name.charAt(i));}System.out.println();}public void out1(String name) {int len = name.length();synchronized (Outputer.class) {for (int i = 0; i < len; i++) {System.out.print(name.charAt(i));}System.out.println();}}public synchronized static void out3(String name) {int len = name.length();for (int i = 0; i < len; i++) {System.out.print(name.charAt(i));}System.out.println();}}}

Naturally, when running, the out and out3 methods cannot be mutually exclusive. When thread 1 executes out1 and thread 2 executes out3, the thread can be mutually exclusive,

So how can we make the out and out3 Methods mutually exclusive? It is not applicable to the out1 method?

Please advise

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.