Results 1 to 2 of 2
Thread: Threads
- 11-20-2007, 02:19 PM #1
Member
- Join Date
- Aug 2007
- Posts
- 15
- Rep Power
- 0
Threads
Hi Guys
I not expert in thread either any programing lanauge.but I still learning java.I got a problem once i wrote some java coding.It's not a syntax error.I want to how know to do this.Below i metion coding
Java Code:public class Test137 { public static void main(String[] args){ String[] str={"TOM","ANNE","RYABN","SAM"}; [COLOR="Red"] for(int i=0;i<str.length;i++){ System.out.println(str[i]); }[/COLOR] [COLOR="Green"] for(int j=str.length-1;j>=0;j--){ System.out.println(str[j]); }[/COLOR] }
in above coding i Highlighted specific segements of coding in Red and Green colors.So what I want to do is using thread I want to do these 2 separate for loops.And u cant use one of element in string once it's used by another thread.it's means
Thread1 cant user str[3] while Thread2 is using str[3].Please help me
Thank YouLast edited by one198; 11-20-2007 at 02:21 PM.
- 11-20-2007, 06:15 PM #2
Java Code:public class Test137Rx { static String[] str={"TOM","ANNE","RYABN","SAM"}; public static void main(String[] args){ new Thread(runner1).start(); new Thread(runner2).start(); } /* private static synchronized String getValue(int index) { return str[index]; } */ private static String getValue(int index) { synchronized(str) { return str[index]; } } private static Runnable runner1 = new Runnable() { public void run() { for(int i=0;i<str.length;i++){ System.out.println("1 " + getValue(i)); } } }; private static Runnable runner2 = new Runnable() { public void run() { for(int j=str.length-1;j>=0;j--){ System.out.println("2 " + getValue(j)); } } }; }
Similar Threads
-
How to use Java threads
By Java Tip in forum java.langReplies: 0Last Post: 04-09-2008, 06:30 PM -
applets & threads
By willemjav in forum Java AppletsReplies: 2Last Post: 04-04-2008, 06:59 AM -
Using threads
By Java Tip in forum Java TipReplies: 0Last Post: 12-11-2007, 10:25 AM -
Server n threads
By ferosh in forum NetworkingReplies: 2Last Post: 04-28-2007, 10:42 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks