Results 1 to 11 of 11
Thread: Thread experiment problem
- 05-18-2011, 02:22 PM #1
Member
- Join Date
- Oct 2010
- Posts
- 33
- Rep Power
- 0
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.
- 05-18-2011, 02:33 PM #2
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.
- 05-18-2011, 02:43 PM #3
Member
- Join Date
- Oct 2010
- Posts
- 33
- Rep Power
- 0
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
- 05-18-2011, 02:43 PM #4
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.
- 05-18-2011, 02:54 PM #5
Member
- Join Date
- Oct 2010
- Posts
- 33
- Rep Power
- 0
- 05-18-2011, 02:56 PM #6
Moderator
- Join Date
- Apr 2009
- Posts
- 10,460
- Rep Power
- 16
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.
- 05-18-2011, 02:59 PM #7
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.
- 05-18-2011, 03:00 PM #8
Member
- Join Date
- Oct 2010
- Posts
- 33
- Rep Power
- 0
- 05-18-2011, 03:44 PM #9
Moderator
- Join Date
- Apr 2009
- Posts
- 10,460
- Rep Power
- 16
Make sure it's run against an Oracle JVM.
I wouldn't necessarily trust the one that came with Eclipse...
- 05-19-2011, 09:12 AM #10
Member
- Join Date
- May 2011
- Posts
- 1
- Rep Power
- 0
- 05-19-2011, 12:18 PM #11
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,400
- Blog Entries
- 7
- Rep Power
- 17
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,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
Similar Threads
-
Thread Problem
By zhein04 in forum Threads and SynchronizationReplies: 2Last Post: 02-07-2011, 12:12 AM -
Programmer genius experiment
By jankidudel in forum New To JavaReplies: 12Last Post: 10-11-2010, 01:34 PM -
Newbie Game Experiment - looking for feedback/advice
By papium in forum New To JavaReplies: 8Last Post: 09-23-2010, 12:48 AM -
Java Program For Web Experiment
By cl1202 in forum New To JavaReplies: 1Last Post: 01-23-2009, 05:17 PM -
Interesting Experiment
By uncommon in forum Advanced JavaReplies: 13Last Post: 12-20-2008, 11:30 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks