Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-6263740

(thread) A thread can be cloned and that breaks getId() contract

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Closed
    • Priority: P4
    • Resolution: Won't Fix
    • Affects Version/s: 6
    • Fix Version/s: None
    • Component/s: core-libs
    • Labels:
    • Subcomponent:
    • CPU:
      x86
    • OS:
      windows_xp

      Description

      FULL PRODUCT VERSION :
      java version "1.6.0-ea"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.6.0-ea-b33)
      Java HotSpot(TM) Client VM (build 1.6.0-ea-b33, mixed mode, sharing)


      ADDITIONAL OS VERSION INFORMATION :
      Microsoft Windows XP [version 5.1.2600]

      A DESCRIPTION OF THE PROBLEM :
      It's possible to clone a thread by creating a subclass of Thread
      that override clone(). A thread and its clone share the same thread id.

      I think that Thread.clone() must be ovverride to :
      solution 1: prohibit clone() by returns a CloneNotSupported exception
      solution 2 : call nextThreadID() to update the thread id.

      The solution 2 avoid to breaks code that already clone threads.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Create a subclass of thread
      Make it Cloneable
      Override clone()
      Create a thread, clone it
      start the two
      they have the same thread id (getId())

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      8567361 5 7
      9584176 5 8
      ACTUAL -
      8567361 5 7
      9584176 5 7


      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      public class CloneThreadTest extends java.lang.Thread implements Cloneable {
        @Override
        public CloneThreadTest clone() throws CloneNotSupportedException {
          return (CloneThreadTest)super.clone();
        }
        @Override
        public void run(){
          for(;;) {
            System.out.println(System.identityHashCode(this)+" "+getPriority()+" "+getId());
            try {
              Thread.sleep(100);
            } catch (InterruptedException e) {
              e.printStackTrace();
            }
          }
        }
        public static void main(String[] args) throws CloneNotSupportedException, InterruptedException{
            CloneThreadTest t = new CloneThreadTest();
            Thread t2=t.clone();
            
            t.start();
            t2.start();
        }
      }
      ---------- END SOURCE ----------
      ###@###.### 2005-04-29 20:40:25 GMT

        Attachments

          Activity

            People

            Assignee:
            psoper Pete Soper (Inactive)
            Reporter:
            gmanwanisunw Girish Manwani (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Dates

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: