Results 1 to 20 of 33
Thread: Help:P i don't even have a title
- 10-01-2011, 07:59 PM #1
Member
- Join Date
- Oct 2011
- Posts
- 18
- Rep Power
- 0
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.
- 10-01-2011, 08:21 PM #2
Re: Help:P i don't even have a title
Are you talking about having the two methods execute at the "same" time, not one after the other?What can i do to make them start the same time...
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.
-
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.
- 10-01-2011, 08:24 PM #4
Member
- Join Date
- Oct 2011
- Posts
- 18
- Rep Power
- 0
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...
- 10-01-2011, 08:25 PM #5
Member
- Join Date
- Oct 2011
- Posts
- 18
- Rep Power
- 0
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
- 10-01-2011, 08:27 PM #6
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.
-
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.
- 10-01-2011, 08:30 PM #8
Member
- Join Date
- Oct 2011
- Posts
- 18
- Rep Power
- 0
Re: Help:P i don't even have a title
and in my many class i have....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) { } }
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...
- 10-01-2011, 08:33 PM #9
Member
- Join Date
- Oct 2011
- Posts
- 18
- Rep Power
- 0
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-01-2011, 08:36 PM #10
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 6Last edited by Norm; 10-01-2011 at 08:40 PM. Reason: removed comments. start is not Thread's start()
-
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.
- 10-01-2011, 08:39 PM #12
Member
- Join Date
- Oct 2011
- Posts
- 18
- Rep Power
- 0
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....
- 10-01-2011, 09:05 PM #13
Member
- Join Date
- Oct 2011
- Posts
- 18
- Rep Power
- 0
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(); } }i am missing something:PJava 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(); } }
- 10-01-2011, 09:08 PM #14
Re: Help:P i don't even have a title
I am not sure what that means.i am missing something:P
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.
- 10-01-2011, 09:10 PM #15
Member
- Join Date
- Oct 2011
- Posts
- 18
- Rep Power
- 0
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
- 10-01-2011, 09:12 PM #16
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.
- 10-01-2011, 09:17 PM #17
Member
- Join Date
- Oct 2011
- Posts
- 18
- Rep Power
- 0
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.....
- 10-01-2011, 09:26 PM #18
Re: Help:P i don't even have a title
When you get the threads working and they call a print method,i want to create a new CastleKeeper object
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.
- 10-01-2011, 09:46 PM #19
Member
- Join Date
- Oct 2011
- Posts
- 18
- Rep Power
- 0
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.
- 10-01-2011, 09:50 PM #20
Re: Help:P i don't even have a title
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.keep getting errors... i am confused with all these classes.
When that works, then change the program to use two different techniques to create the threads.
Similar Threads
-
Not sure how to name this title so i'll just use this, HELP!
By lordarnoud in forum Advanced JavaReplies: 19Last Post: 06-10-2011, 07:58 PM -
Title not displayer
By weezy2894 in forum Java AppletsReplies: 13Last Post: 04-18-2011, 04:12 AM -
Change the title
By AndrewSD in forum NetBeansReplies: 1Last Post: 01-11-2011, 03:23 AM -
set * at tab title
By keffie91 in forum Advanced JavaReplies: 7Last Post: 10-06-2008, 07:21 PM


2Likes
LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks