Results 1 to 3 of 3
Thread: multithreading basics
- 04-14-2011, 01:43 PM #1
Member
- Join Date
- Mar 2011
- Posts
- 45
- Rep Power
- 0
multithreading basics
Threads in this program should interfere with each other because of accessing the same array at the time. So output should rather look like "12112" than like "22222", right? The thing is that output IS actually "22222", but I cannot see the reason.
Java Code:public class Threads { public static void main(String[] args) { PrintTask1 thread1 = new PrintTask1(); PrintTask2 thread2 = new PrintTask2(); thread1.run(); //These two threads should interfere thread2.run(); //with each other and provide wrong Array.show(); //output in this line. } } class PrintTask1 implements Runnable { //Nothing complicated, just public void run() { //modifying the array for(int i=0;i<5;i++) { Array.array[i]=1; } } } class PrintTask2 implements Runnable { //The same public void run() { for(int i=0;i<5;i++) { Array.array[i]=2; } } } class Array { //Data array, or in this case, so called "buffer" public static int[] array = new int[5]; public static void show() { for(int i=0;i<5;i++) System.out.println(array[i]); } }
- 04-14-2011, 01:46 PM #2
You aren't actually creating any new Threads. Instances of Runnable are not automatically run on a new Thread.
How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
- 04-14-2011, 01:55 PM #3
Member
- Join Date
- Mar 2011
- Posts
- 45
- Rep Power
- 0
Similar Threads
-
Multithreading Gui
By BUGSIE91 in forum Threads and SynchronizationReplies: 7Last Post: 10-13-2010, 02:20 PM -
Want to know about Multithreading.
By Chetans in forum Threads and SynchronizationReplies: 1Last Post: 03-19-2010, 07:50 AM -
Really Basics
By Taluntain in forum New To JavaReplies: 16Last Post: 10-08-2009, 09:43 AM -
Basics
By AKP in forum New To JavaReplies: 7Last Post: 05-23-2008, 12:06 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks