Results 1 to 11 of 11
  1. #1
    jeata is offline Member
    Join Date
    Oct 2010
    Posts
    33
    Rep Power
    0

    Default Thread experiment problem

    I've been playing around with threads, but I don't quite get the results I'm looking for.

    I have 3 classes

    VariablesClass
    Has a variable ”number” = the number indicated by the wheel of fortune.
    There are four numbers on the wheel of fortune : 1, 2, 3 and 4.

    ThreadsClass
    A thread spinning the wheel of fortune.
    Each pass will move the wheel of fortune one place forward.

    TestClass
    Creates a wheeloffortune and 3 threads.
    Starts the threads, but do not stop them.

    Sometimes I get only 2 threads to run, sometimes I get a result saying I get 7,6 for all the threads, sometimes I get an exception.

    Here are the classes, it shouldn't be able to get a result bigger than 4.

    Java Code:
    public class VariablesClass
    {
        // instance variables 
        public int ONE=1;
        public int TWO=2;
        public int THREE=3;
        public int FOUR=4;
        private int number;
    
        // constructor
        public VariablesClass()
        {
            number=ONE;
        }
    
        // get number
        public int getNumber()
        {
            return number;
        }
        
        // set number
        public void setNumber(int n)
        {
            number=n;
        }
        
    }
    Java Code:
    public class ThreadsClass extends Thread
    {
        // instance variables 
        boolean running;
        VariablesClass fortune;
        int id;
        long starttime;
        long endtime;
        DecimalFormat fmt=new DecimalFormat("###0.00");
    
        // constructor
        public ThreadsClass (VariablesClass fortune, int id)
        {
            this.fortune=fortune;
            this.id=id;
            starttime=System.currentTimeMillis();
            running=true;
        }
    
        // run
        public void run()
        {
            while(running)
            {
                if(fortune.getNumber()==fortune.FOUR)
                {
                    fortune.setNumber(fortune.ONE);
                }
                else
                {
                    fortune.setNumber(fortune.getNumber()+1);
                    if(fortune.getNumber()>fortune.FOUR)
                    {
                        endtime=System.currentTimeMillis();
                        System.out.println("Thread "+id+" stopped after "+fmt.format(0.001*(endtime-starttime))+" sec");
                        System.out.println("number is now = "+fortune.getNumber());
                        stopThread();
                    }
                }
            }
        }
        
        // stop
        public void stopThread()
        {
            running=false;
        }
        
    }
    Java Code:
    public class TestClass
        // main
        public static void main(String[] args)
        {
            VariablesClass wof=new VariablesClass();
            ThreadsClass a=new ThreadsClass(wof,1);
            ThreadsClass b=new ThreadsClass(wof,2);
            ThreadsClass c=new ThreadsClass(wof,3);
            a.start();
            b.start();
            c.start();
         }
        
    }
    Last edited by jeata; 05-18-2011 at 02:44 PM.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,562
    Rep Power
    25

    Default

    The code you posted does not compile: ThreadsClass vs ThreadClass

    Please copy and paste here the output from the program and add some comments showing what the problem is.

    I get this:
    Thread 2 stopped after 1.77 sec
    number is now = 5
    Thread 3 stopped after 1.77 sec
    number is now = 6
    Thread 1 stopped after 1.77 sec
    number is now = 7

    0 error(s)


    Try debugging your code by adding println() statements in various methods to show variable values and program flow.
    You will quickly see what the problem is.
    Last edited by Norm; 05-18-2011 at 02:42 PM.

  3. #3
    jeata is offline Member
    Join Date
    Oct 2010
    Posts
    33
    Rep Power
    0

    Default

    Hi

    Here's a couple of compile results

    Java Code:
    Thread 2 stopped after 0.00 sec
    number is now = 2
    Thread 3 stopped after 0.00 sec
    number is now = 1
    Java Code:
    Thread 1 stopped after 0.02 sec
    number is now = 5
    Thread 3 stopped after 0.01 sec
    number is now = 5
    Thread 2 stopped after 0.01 sec
    number is now = 5
    Unhandled exception
    Type=Segmentation error vmState=0x00000000
    J9Generic_Signal_Number=00000004 ExceptionCode=c0000005 ExceptionAddress=02ACD15F ContextFlags=0001003f
    Handler1=002EA090 Handler2=001EBE40 InaccessibleAddress=0064EEE4
    EDI=005C7FF8 ESI=00601CDC EAX=0064EEE4 EBX=005C8F04
    ECX=00000002 EDX=001D313C
    EIP=02ACD15F ESP=0056FB3C EBP=001B6A10 EFLAGS=00210206
    GS=002B FS=0053 ES=002B DS=002B
    Module=D:\Rational Architect\RAT\jdk\jre\bin\j9jvmti24.dll
    Module_base_address=02AC0000 Offset_in_DLL=0000d15f
    Target=2_40_20091125_047905 (Windows 7 6.1 build 7600)
    CPU=x86 (4 logical CPUs) (0xb7486000 RAM)
    ----------- Stack Backtrace -----------
    disposeEnvironment+0xbf (jvmtihelpers.c:216, 0x02ACD15F [j9jvmti24+0xd15f])
    shutDownJVMTI+0x58 (jvmtistartup.c:493, 0x02AD6D88 [j9jvmti24+0x16d88])
    J9VMDllMain+0x369 (jvmtistartup.c:201, 0x02AD73A9 [j9jvmti24+0x173a9])
    runJ9VMDllMain+0xcd (jvminit.c:2483, 0x003069AD [j9vm24+0x269ad])
    pool_do+0x54 (pool.c:432, 0x003281C4 [j9vm24+0x481c4])
    runShutdownStage+0x53 (jvminit.c:2865, 0x0030AEF3 [j9vm24+0x2aef3])
    freeJavaVM+0x178 (jvminit.c:862, 0x0030C058 [j9vm24+0x2c058])
    protectedDestroyJavaVM+0x3cc (jniinv.c:421, 0x002FA95C [j9vm24+0x1a95c])
    j9sig_protect+0x41 (j9signal.c:144, 0x001EBFA1 [J9PRT24+0xbfa1])
    DestroyJavaVM+0x189 (jniinv.c:494, 0x002FABA9 [j9vm24+0x1aba9])
    DestroyJavaVM+0xc (jvm.c:402, 0x001A130C [jvm+0x130c])
    (0x00403748 [javaw+0x3748])
    (0x00409605 [javaw+0x9605])
    BaseThreadInitThunk+0x12 (0x75413677 [kernel32+0x13677])
    RtlInitializeExceptionChain+0x63 (0x77489D72 [ntdll+0x39d72])
    RtlInitializeExceptionChain+0x36 (0x77489D45 [ntdll+0x39d45])
    ---------------------------------------
    JVMDUMP006I Processing dump event "gpf", detail "" - please wait.
    JVMDUMP013I Processed dump event "gpf", detail "".
    Java Code:
    Thread 3 stopped after 0.01 sec
    number is now = 2
    Thread 2 stopped after 1.57 sec
    number is now = 5
    Thread 1 stopped after 1.58 sec
    number is now = 5
    Unhandled exception
    Type=Segmentation error vmState=0x00000000
    J9Generic_Signal_Number=00000004 ExceptionCode=c0000005 ExceptionAddress=026CD15F ContextFlags=0001003f
    Handler1=001CA090 Handler2=0023BE40 InaccessibleAddress=0067EEE4
    EDI=005F7FF8 ESI=00631CDC EAX=0067EEE4 EBX=005F8F04
    ECX=00000002 EDX=0022313C
    EIP=026CD15F ESP=003BFB3C EBP=001B6A10 EFLAGS=00210206
    GS=002B FS=0053 ES=002B DS=002B
    Module=D:\Rational Architect\RAT\jdk\jre\bin\j9jvmti24.dll
    Module_base_address=026C0000 Offset_in_DLL=0000d15f
    Target=2_40_20091125_047905 (Windows 7 6.1 build 7600)
    CPU=x86 (4 logical CPUs) (0xb7486000 RAM)
    ----------- Stack Backtrace -----------
    disposeEnvironment+0xbf (jvmtihelpers.c:216, 0x026CD15F [j9jvmti24+0xd15f])
    shutDownJVMTI+0x58 (jvmtistartup.c:493, 0x026D6D88 [j9jvmti24+0x16d88])
    J9VMDllMain+0x369 (jvmtistartup.c:201, 0x026D73A9 [j9jvmti24+0x173a9])
    runJ9VMDllMain+0xcd (jvminit.c:2483, 0x001E69AD [j9vm24+0x269ad])
    pool_do+0x54 (pool.c:432, 0x002081C4 [j9vm24+0x481c4])
    runShutdownStage+0x53 (jvminit.c:2865, 0x001EAEF3 [j9vm24+0x2aef3])
    freeJavaVM+0x178 (jvminit.c:862, 0x001EC058 [j9vm24+0x2c058])
    protectedDestroyJavaVM+0x3cc (jniinv.c:421, 0x001DA95C [j9vm24+0x1a95c])
    j9sig_protect+0x41 (j9signal.c:144, 0x0023BFA1 [J9PRT24+0xbfa1])
    DestroyJavaVM+0x189 (jniinv.c:494, 0x001DABA9 [j9vm24+0x1aba9])
    DestroyJavaVM+0xc (jvm.c:402, 0x001A130C [jvm+0x130c])
    (0x00403748 [javaw+0x3748])
    (0x00409605 [javaw+0x9605])
    BaseThreadInitThunk+0x12 (0x75413677 [kernel32+0x13677])
    RtlInitializeExceptionChain+0x63 (0x77489D72 [ntdll+0x39d72])
    RtlInitializeExceptionChain+0x36 (0x77489D45 [ntdll+0x39d45])
    ---------------------------------------
    JVMDUMP006I Processing dump event "gpf", detail "" - please wait.
    JVMDUMP013I Processed dump event "gpf", detail "".
    Java Code:
    Thread 2 stopped after 0.00 sec
    number is now = 1
    Process model delta has encountered a problem

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,562
    Rep Power
    25

    Default

    Try debugging your code by adding println() statements in various methods to show variable values and program flow.
    You will quickly see what the problem is.

  5. #5
    jeata is offline Member
    Join Date
    Oct 2010
    Posts
    33
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    Try debugging your code by adding println() statements in various methods to show variable values and program flow.
    You will quickly see what the problem is.
    Hmm, where exactly, I really have no idea about threads.

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,091
    Rep Power
    20

    Default

    Is that a Rational-specific JRE, or is that simply an Oracle one supplied with the RAT?
    If the former, have you tried running this with an Oracle JRE?

    Because that's not an exception...that's your JVM throwing a wobbly.

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,562
    Rep Power
    25

    Default

    Put a println in any of the methods that are called and that change values and that test values.
    You want to see where the execution flow goes and how the controlling variables values change.

  8. #8
    jeata is offline Member
    Join Date
    Oct 2010
    Posts
    33
    Rep Power
    0

    Default

    Quote Originally Posted by Tolls View Post
    Is that a Rational-specific JRE, or is that simply an Oracle one supplied with the RAT?
    If the former, have you tried running this with an Oracle JRE?

    Because that's not an exception...that's your JVM throwing a wobbly.
    Good idea, I'm switching to ubuntu now, to try to compile it in eclipse.

  9. #9
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,091
    Rep Power
    20

    Default

    Make sure it's run against an Oracle JVM.
    I wouldn't necessarily trust the one that came with Eclipse...

  10. #10
    applegigs is offline Member
    Join Date
    May 2011
    Posts
    1
    Rep Power
    0

    Default :D

    Isn't this the CA from Knord 2011 Threads ?
    the code look identical .
    Quote Originally Posted by jeata View Post
    I've been playing around with threads, but I don't quite get the results I'm looking for.

    I have 3 classes

    VariablesClass
    Has a variable ”number” = the number indicated by the wheel of fortune.
    There are four numbers on the wheel of fortune : 1, 2, 3 and 4.

    ThreadsClass
    A thread spinning the wheel of fortune.
    Each pass will move the wheel of fortune one place forward.

    TestClass
    Creates a wheeloffortune and 3 threads.
    Starts the threads, but do not stop them.

    Sometimes I get only 2 threads to run, sometimes I get a result saying I get 7,6 for all the threads, sometimes I get an exception.

    Here are the classes, it shouldn't be able to get a result bigger than 4.

    Java Code:
    public class VariablesClass
    {
        // instance variables 
        public int ONE=1;
        public int TWO=2;
        public int THREE=3;
        public int FOUR=4;
        private int number;
    
        // constructor
        public VariablesClass()
        {
            number=ONE;
        }
    
        // get number
        public int getNumber()
        {
            return number;
        }
        
        // set number
        public void setNumber(int n)
        {
            number=n;
        }
        
    }
    Java Code:
    public class ThreadsClass extends Thread
    {
        // instance variables 
        boolean running;
        VariablesClass fortune;
        int id;
        long starttime;
        long endtime;
        DecimalFormat fmt=new DecimalFormat("###0.00");
    
        // constructor
        public ThreadsClass (VariablesClass fortune, int id)
        {
            this.fortune=fortune;
            this.id=id;
            starttime=System.currentTimeMillis();
            running=true;
        }
    
        // run
        public void run()
        {
            while(running)
            {
                if(fortune.getNumber()==fortune.FOUR)
                {
                    fortune.setNumber(fortune.ONE);
                }
                else
                {
                    fortune.setNumber(fortune.getNumber()+1);
                    if(fortune.getNumber()>fortune.FOUR)
                    {
                        endtime=System.currentTimeMillis();
                        System.out.println("Thread "+id+" stopped after "+fmt.format(0.001*(endtime-starttime))+" sec");
                        System.out.println("number is now = "+fortune.getNumber());
                        stopThread();
                    }
                }
            }
        }
        
        // stop
        public void stopThread()
        {
            running=false;
        }
        
    }
    Java Code:
    public class TestClass
        // main
        public static void main(String[] args)
        {
            VariablesClass wof=new VariablesClass();
            ThreadsClass a=new ThreadsClass(wof,1);
            ThreadsClass b=new ThreadsClass(wof,2);
            ThreadsClass c=new ThreadsClass(wof,3);
            a.start();
            b.start();
            c.start();
         }
        
    }

  11. #11
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,655
    Blog Entries
    7
    Rep Power
    21

    Default

    The dump definitely is caused by a bug somewhere; incorrect Thread management should never cause it. Your 'number' variable is a shared resource so all reads and writes should occur in a synchronized method in your VariablesClass. Consequently, increment and wrap the number in a synchronized method in that class.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Thread Problem
    By zhein04 in forum Threads and Synchronization
    Replies: 2
    Last Post: 02-07-2011, 12:12 AM
  2. Programmer genius experiment
    By jankidudel in forum New To Java
    Replies: 12
    Last Post: 10-11-2010, 01:34 PM
  3. Replies: 8
    Last Post: 09-23-2010, 12:48 AM
  4. Java Program For Web Experiment
    By cl1202 in forum New To Java
    Replies: 1
    Last Post: 01-23-2009, 05:17 PM
  5. Interesting Experiment
    By uncommon in forum Advanced Java
    Replies: 13
    Last Post: 12-20-2008, 11:30 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •