Page 1 of 2 12 LastLast
Results 1 to 20 of 33
Like Tree2Likes

Thread: Help:P i don't even have a title

  1. #1
    Ophelia is offline Member
    Join Date
    Oct 2011
    Posts
    18
    Rep Power
    0

    Default Help:P i don't even have a title

    Hello people.
    I have a project for my university. And i am trying many days know to make it work .... but i cant... Long story short... (?)

    i have a TypeA class which extents and implements some other classes..In that class there is a public void start() method which has a for loop.
    I also have a i have a TypeB class which extents and implements some other classes..In that class there is a public void start() method which has a for loop. the difference in these two classes is the start method, alla the other things are the same...

    Now, when i go to main and insert
    TypeA QWERTY = new TypeA("blabla");
    QWERTY.start();
    TypeB ASDF = new TypeA("blabla");
    ASDF.start();

    What happens when i run this is that the QWERTY.start() runs until the for loop finish and then starts the ASDF.start().

    What can i do to make them start the same time...
    I tried many combinations and i got lost....

    btw the code is for a university application. The java connects to the app with a certain port.

    Thank you all.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,331
    Rep Power
    25

    Default Re: Help:P i don't even have a title

    What can i do to make them start the same time...
    Are you talking about having the two methods execute at the "same" time, not one after the other?
    You can create threads for the calls to each method and start the threads. They will sort of run in parallel depending on how the OS and JVM give them CPU cycles.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Help:P i don't even have a title

    There's too much that you aren't telling us. Do either of these classes extend Thread or Runnable? Perhaps you should show us more code, but if you do, please use code tags so that your posted code retains its formatting and is readable by placing the tag [code] on top of your code block and the tag [/code] below your code block.

  4. #4
    Ophelia is offline Member
    Join Date
    Oct 2011
    Posts
    18
    Rep Power
    0

    Default Re: Help:P i don't even have a title

    I knew that i had to create threads... :(:(:( i also tried... but i am new to java and i have no idea... My class extends some other classes and i dont know how to make this work... when i tried i had errors...

  5. #5
    Ophelia is offline Member
    Join Date
    Oct 2011
    Posts
    18
    Rep Power
    0

    Default Re: Help:P i don't even have a title

    i want to extend thread but i cannot because they extend other classes..... and i don't know how to extend it :P do i sound silly:P

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,331
    Rep Power
    25

    Default Re: Help:P i don't even have a title

    You do not need to extend Thread. You could implement Runnable or you could use another class that extends Thread or use an anonymous Runnable class with the Thread class. Your choice.

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Help:P i don't even have a title

    Nothing personal, but it sounds like you're completely lost. The best thing to do at this time is contact your instructor and have a chat. You need some one-on-one face-to-face time as this offers you the best chance to get you up to speed.

  8. #8
    Ophelia is offline Member
    Join Date
    Oct 2011
    Posts
    18
    Rep Power
    0

    Default Re: Help:P i don't even have a title

    Java Code:
    public class CastleKeeper1 extends BaseMotileAgent implements MotileAgentListener{
        
        /**
         * The agent's GUI.
         */
    
    
        public CastleKeeper1(String name) {
    
            super(name);
    
            if (status.equals(Status.INITIALIZED)) {
    
                System.out.println("[" + getName() + "] Initialized.");
    
                addMotileAgentListener(this);
    
                System.out.print("[" + getName() + "] Creating spatial representation window... ");
    
    
                System.out.print("[" + getName() + "] Connecting... ");
                try {
                    connect("127.0.0.1", 4444);
                }
                catch (IOException ex) {
                    System.out.println("\n[" + getName() + "] Network error while connecting (" + ex.getMessage() + ")");
                }
    
                if (status.equals(Status.CONNECTED)) {
                    System.out.println("Done.");
                    System.out.print("[" + getName() + "] Attaching motor effector to motor accesspoint... ");
                    act("attach motor motorAccessPoint");
                    System.out.println("Done.");
    
                    System.out.print("[" + getName() + "] Selecting translation action... ");
                    act("select motor move translate");
                    System.out.println("Done.");
    
                    System.out.print("[" + getName() + "] Selecting rotation action... ");
                    act("select motor turn rotate");
                    System.out.println("Done.");
    
                    sense();
                    update();
                }
            }
            
        }
    
        public void start(){
            
            int count = 1;
            do {
            setSpeed(10);
            moveTo(0,0);
            
                setSpeed(3);
                moveTo(-2,0);
                moveTo(-2,-4);
                moveTo(8,-4);
                moveTo(8,5);
                moveTo(-1,5);
                count=count+1;
            }
            while (count<=2); 
            
        } 
    
        @Override
        public void translationCompleted(MotileAgentEvent ev) {
            System.out.println("[" + getName() + "] Translation completed.");
            sense();
            update();
           
        }
    
        
        @Override
        public void rotationCompleted(MotileAgentEvent ev) {
            System.out.println("[" + getName() + "] Rotation completed.");
            sense();
            update();
        }
    
        
        @Override
        public void segmentStarted(MotileAgentEvent ev) {
            System.out.println("[" + getName() + "] Segment started.");
            sense();
            update();
    
        }
    
       
        @Override
        public void segmentCompleted(MotileAgentEvent ev) {
        }
    
        
        @Override
        public void pathCompleted(MotileAgentEvent ev) {
        }
    
    }
    and in my many class i have....


    public class MainClass extends Thread {
    public static void main(String[] args) {
    CastleKeeper1 CK1 = new CastleKeeper1("CK1");
    CK1.start();
    }

    i want to call another class the same as the above CastleKeeper1 but with a different start. And i want them to run at the same time...

  9. #9
    Ophelia is offline Member
    Join Date
    Oct 2011
    Posts
    18
    Rep Power
    0

    Default Re: Help:P i don't even have a title

    i've been trying to contact but he is lost... i think that he did not came back from his vacations... :/

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,331
    Rep Power
    25

    Default Re: Help:P i don't even have a title

    Look at the API doc for the Thread class. It has a couple of examples of how to create and start a thread.
    Java Platform SE 6
    Last edited by Norm; 10-01-2011 at 08:40 PM. Reason: removed comments. start is not Thread's start()

  11. #11
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Help:P i don't even have a title

    I'm with Norm, create two threads, and in each thread, create an anonymous inner Runnable class, and in the run method of each Runnable, call your start method on the object of interest. Then call start() on the Threads, and bingo, you're in business.

  12. #12
    Ophelia is offline Member
    Join Date
    Oct 2011
    Posts
    18
    Rep Power
    0

    Default Re: Help:P i don't even have a title

    ok i tried this.. and it did not work. I was so stupid and i deleted this. I will try this again and i will post back here. Thank you. :) The lesson is not java... and i hardly know any java at all....

  13. #13
    Ophelia is offline Member
    Join Date
    Oct 2011
    Posts
    18
    Rep Power
    0

    Default Re: Help:P i don't even have a title

    Java Code:
    package sample_sara;
    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.io.IOException;
    import javax.swing.JFrame;
    import reve.sara.agent.BaseMotileAgent;
    import reve.sara.agent.MotileAgentEvent;
    import reve.sara.agent.MotileAgentListener;
    import reve.sara.helpers.NavMesh2D;
    import reve.sara.helpers.NavMesh2DView;
    
    public class CastleKeeper extends BaseMotileAgent implements MotileAgentListener {
        
     public CastleKeeper(String name) {
    
            super(name);
    
            if (status.equals(Status.INITIALIZED)) {
    
                System.out.println("[" + getName() + "] Initialized.");
                System.out.print("[" + getName() + "] Creating spatial representation window... ");
    
                System.out.print("[" + getName() + "] Connecting... ");
                try {
                    connect("127.0.0.1", 4444);
                }
                catch (IOException ex) {
                    System.out.println("\n[" + getName() + "] Network error while connecting (" + ex.getMessage() + ")");
                }
    
                if (status.equals(Status.CONNECTED)) {
                    System.out.println("Done.");
                    System.out.print("[" + getName() + "] Attaching motor effector to motor accesspoint... ");
                    act("attach motor motorAccessPoint");
                    System.out.println("Done.");
    
                    System.out.print("[" + getName() + "] Selecting translation action... ");
                    act("select motor move translate");
                    System.out.println("Done.");
    
                    System.out.print("[" + getName() + "] Selecting rotation action... ");
                    act("select motor turn rotate");
                    System.out.println("Done.");
    
                    sense();
                    update();
                }
            }
           // frame.repaint();
        }
     
     
    public class Thread1 implements Runnable  {
        public void run() {
            setSpeed(10);
            moveTo(0,0);
            for (int i=0;i!=100;i++) {
                setSpeed(3);
                moveTo(-2,0);
                moveTo(-2,-4);
                moveTo(8,-4);
                moveTo(8,5);
                moveTo(-1,5);
                }
            }
        }
    
    
        
           
    
     @Override
        public void translationCompleted(MotileAgentEvent ev) {
            System.out.println("[" + getName() + "] Translation completed.");
            sense();
            update();
            //frame.repaint();
        }
    
    
        @Override
        public void rotationCompleted(MotileAgentEvent ev) {
            System.out.println("[" + getName() + "] Rotation completed.");
            sense();
            update();
            //frame.repaint();
        }
    
        @Override
        public void segmentStarted(MotileAgentEvent ev) {
        }
    
    
        @Override
        public void segmentCompleted(MotileAgentEvent ev) {
        }
    
    
        @Override
        public void pathCompleted(MotileAgentEvent ev) {
            System.out.println("[" + getName() + "] Bye bye!");
            hangup();
        }
    
    }
    Java Code:
    package sample_sara;
    import sample_sara.CastleKeeper.Thread1;
    
    
    /**
     *
     * @author Stella
     */
    
    public class MainClass extends Thread {
         public static void main(String[] args) {
            Runnable as = new Thread1();
            Thread1 thread = new Thread1(as);
            thread.start();
        } 
    }
    i am missing something:P

  14. #14
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,331
    Rep Power
    25

    Default Re: Help:P i don't even have a title

    i am missing something:P
    I am not sure what that means.

    Try making a very simple program that creates two threads each of which prints out a message and exits. Trying to add code to this larger program looks like it is confusing you.

  15. #15
    Ophelia is offline Member
    Join Date
    Oct 2011
    Posts
    18
    Rep Power
    0

    Default Re: Help:P i don't even have a title

    but i need this to work in this program... and i am almost 12 hours in my pc :P

  16. #16
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,331
    Rep Power
    25

    Default Re: Help:P i don't even have a title

    You need to understand the technique first. When you can successfully get some threads to work then you can take what you have learned and copy it into the larger program.

    Lots of programmers use that technique to work out how to code new classes. Using a small easy to work with class, get it to work then merge the logic and techniques into the large program.
    Fubarable and Solarsonic like this.

  17. #17
    Ophelia is offline Member
    Join Date
    Oct 2011
    Posts
    18
    Rep Power
    0

    Default Re: Help:P i don't even have a title

    i have seen examples that print numbers and letters i just dont understant what my input in the thread will be in my example.... Because i want to create a new CastleKeeper object with the using the run of the thread.....

  18. #18
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,331
    Rep Power
    25

    Default Re: Help:P i don't even have a title

    i want to create a new CastleKeeper object
    When you get the threads working and they call a print method,
    Then you can replace the print statement with code to create the new object.

    Your current problem is how to create two threads and have them execute at the same time.
    What the threads do initially is not important to the task of creating the threads and getting them to work.

  19. #19
    Ophelia is offline Member
    Join Date
    Oct 2011
    Posts
    18
    Rep Power
    0

    Default Re: Help:P i don't even have a title

    my problem right now is that i cannot even create one thread. I keep getting errors... i am confused with all these classes.

  20. #20
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,331
    Rep Power
    25

    Default Re: Help:P i don't even have a title

    keep getting errors... i am confused with all these classes.
    That is exactly why I still suggest that you put the big program aside and write a small test program that creates two threads that do something simple like print out a message.
    When that works, then change the program to use two different techniques to create the threads.

Page 1 of 2 12 LastLast

Similar Threads

  1. Replies: 19
    Last Post: 06-10-2011, 07:58 PM
  2. Title not displayer
    By weezy2894 in forum Java Applets
    Replies: 13
    Last Post: 04-18-2011, 04:12 AM
  3. Change the title
    By AndrewSD in forum NetBeans
    Replies: 1
    Last Post: 01-11-2011, 03:23 AM
  4. set * at tab title
    By keffie91 in forum Advanced Java
    Replies: 7
    Last Post: 10-06-2008, 07:21 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
  •