Each thread is used separately for the reference variable of the object?

Source: Internet
Author: User

Package thread;import org.omg.portableserver.thread_policy_id;import java.util.arraylist;import  java.util.HashMap;import java.util.concurrent.locks.Condition;import  java.util.concurrent.locks.reentrantlock;class node{    public int num;     node (int nums) {        num=nums;     }    public node next;} class mytest implements runnable{    string s=null;     node n1=new node (1);     node n2=new node (2);     node n3=new node (3);     @Override     public void  run ()  {        n1.next=n2;         n2.next=n3;        n3.next=null;        node e=n1;         while (e!=null) {            node n1= E.next;            system.out.println ("Current thread is" + Thread.CurrentThread (). GetName ());             if ( Thread.CurrentThread (). GetName (). Equals ("Thread-0")) {                 try {                     system.out.println ("Thread-0 to block");                      thread.sleep (10000);                 } catch  (IntERRUPTEDEXCEPTION E1)  {                     e1.printstacktrace ();                 }                 if (e==null) {                     system.out.println ("E is not owned separately" in the thread);                 }                 else {                      System.out.println ("individually owned");                    &nBsp System.out.println (E.num);                 }            }             system.out.println (Thread.CurrentThread (). GetName () + + + + E.num);            e=e.next;             if (e==null) {                 system.out.println ("in" +thread.currentthread (). GetName () + "E is already empty");            }         }    }}public class Test1 {     Public static void main (String[] args)  {         mytest mytEst=new mytest ();         thread t1=new thread (myTest);         thread t2=new thread (myTest);         t1.start ();         t2.start ();     }}

The result of this code operation is as follows!




It turns out that each thread has a separate reference to the Run Method!

Note that if I declare node1 as a method of a class, it will not be owned by the thread alone!

Each thread is used separately for the reference variable of the object?

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.