Multithreading: Shared Variables __ Multithreading

Source: Internet
Author: User
Tags compact object object

Variable sharing is a way to see how threads are created

Package sample1; public class Sharevariable {public static void main (string[] args) {System.out.println (Thread.currentthrea

        D (). GetName ());
        Data not shared//MYTHREADFORV a = new Mythreadforv ("a");
        Mythreadforv B = new Mythreadforv ("B");
        Mythreadforv C = new Mythreadforv ("C");
        A.start ();
        B.start ();

        C.start ();
        Data sharing: Extends Thread//Mythreadforv t = new Mythreadforv ();
        Thread A = new thread (t, "a");
        Thread B = new Thread (t, "B");
        Thread c = new Thread (t, "C");
        Thread d = new Thread (t, "D");
        Thread e = new Thread (t, "E");
        Thread F = new Thread (T, "F");
        A.start ();
        B.start ();
        C.start ();
        D.start ();
        E.start ();

        F.start ();
        Data not shared: Implement Runnable MYRUNNABLEFORV r1 = new Myrunnableforv ();
MYRUNNABLEFORV r2 = new Myrunnableforv ();    //   MYRUNNABLEFORV R3 = new Myrunnableforv ();
MYRUNNABLEFORV R4 = new Myrunnableforv ();
R1.run ();
R2.run ();
R3.run ();

        R4.run ();
        Data sharing: Implement Runnable thread A = new thread (r1, "a");
        Thread B = new Thread (r1, "B");
        A.start ();

    B.start ();

    Class Mythreadforv extends Thread {private int count = 5;
    Public Mythreadforv () {} public Mythreadforv (String name) {this.setname (name);
            @Override public void Run () {while (Count > 0) {count--;
        System.out.println ("by" + CurrentThread (). GetName () + "calculated, count=" + count);

    Class Myrunnableforv implements Runnable {private int count = 5;
    public void setcount (int num) {this.count = num;
        Public Myrunnableforv () {} @Override public void Run () {//Super.run (); while (Count > 0) {count--;
            System.out.println ("count=" + count);
 }
    }
}

Either inheritance thread or implementation runnable can share member variables, looking at the instantiation of the new thread (T1), new thread (T2)

If you use a shared variable, the above thread method is unsafe and can be preceded by a synchronized (synchronous lock) before public void Run () {}. Because the JVM operates on a multithreaded operation, the I-Compact of the variables is:
1, read the original I,
2, calculate I-compact,
3. Re-assign the value of I.
Multiple threads may interfere with each other without synchronizing the lock.
For example: T1 to the second step, T2 start the first step. This must be a problem. When the lock is added, the T1 begins, and I is locked. T2 must wait until T1 is finished before starting the first step.

Synchronized locks can be used on arbitrary objects and methods.

In Java, you cannot declare a variable directly using synchronized, but instead use synchronized to modify a block of code or a method.
Detailed description is as follows:
Synchronized is used to modify a method or a block of code that ensures that at most one thread at a time executes that segment of code.
First, when two concurrent threads access the synchronized (this) synchronized code block in the same object objects, only one thread can be executed at a time. Another thread must wait until the current thread finishes executing the code block before executing the code block.
Second, however, when a thread accesses a synchronized (this) synchronization code block of object, another thread can still access a non-synchronized (this) synchronized code block in that object.
Third, it is particularly critical that when a thread accesses a synchronized (this) synchronization code block of object, access to all other synchronized (this) synchronized code blocks in object is blocked by other threads.
The third example also applies to other synchronized code blocks. That is, when a thread accesses a synchronized (this) synchronization code block of object, it obtains the object lock of the objects. As a result, access to all of the synchronization code portions of the object object by other threads is temporarily blocked.
The above rules are also applicable to other object locks.

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.