Results 1 to 2 of 2

Thread: Threads

  1. #1
    one198 is offline Member
    Join Date
    Aug 2007
    Posts
    15
    Rep Power
    0

    Default 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 You
    Last edited by one198; 11-20-2007 at 02:21 PM.

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    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

  1. How to use Java threads
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-09-2008, 06:30 PM
  2. applets & threads
    By willemjav in forum Java Applets
    Replies: 2
    Last Post: 04-04-2008, 06:59 AM
  3. Using threads
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-11-2007, 10:25 AM
  4. Server n threads
    By ferosh in forum Networking
    Replies: 2
    Last Post: 04-28-2007, 10:42 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
  •