codetoad.com
  ASP Shopping CartForum & BBS
  - all for $20 from CodeToad Plus!
  
  Home || ASP | ASP.Net | C++/C# | DHTML | HTML | Java | Javascript | Perl | VB | XML || CodeToad Plus! || Forums || RAM 
Search Site:
Search Forums:
  Array and Threads  ericwi at 10:45 on Tuesday, January 23, 2007
 

Hi all,

This may sound trival but does anyone has examples on declaring an array of Threads?

Thanks


  Re: Array and Threads  mmarab at 12:39 on Tuesday, January 23, 2007
 

There you go:

import java.lang.*;

public class thready implements Runnable {

public void run(){
Thread[] mythreay = new Thread[20];
}

}

  Re: Array and Threads  ericwi at 05:36 on Wednesday, January 24, 2007
 

Thanks for your help mmarab,


Does your solution also applies to this :-

MyConstructor newObj = new MyConstructor( "name" , age);
.
.
.
Thread newObj = new Thread[5];
.
.
.
newObj[0].start();
newObj[1].start();
.
.


Is this legal? Anyone can verify this can work?

Thanks




  Re: Array and Threads  mmarab at 08:38 on Wednesday, January 24, 2007
 

Yes that should work fine!

  Re: Array and Threads  ericwi at 01:38 on Thursday, January 25, 2007
 

Hi ,

When I try to complie, it prompt the error
" cannot find symbol. class MyConstructor. netObj[x].start()". Do you all think my start() statement should be in the class MyConstructor although there is no main(String[] args) method?

Thanks
Eric


  Re: Array and Threads  mmarab at 08:53 on Thursday, January 25, 2007
 

Ok this is your code :

[start]
MyConstructor newObj = new MyConstructor( "name" , age);
.
.
.
Thread newObj = new Thread[5];
.
.
.
newObj[0].start();
newObj[1].start();
[end]

From this i guess you have a class called MyConstructor, you create an object from it called newObj, but then you try and make a thread from that class by using the same variable newObj as a thread. This is incorrect. Try this:

import java.lang.*;
public class TestThread extends Thread{

public TestThread() {

}

public static void main(String[] args){
TestThread[] myThread = new TestThread[10];
myThread[0].start();
myThread[1].start();
}
}

this class extends thread, which means it has thread attributes, it can be treated like a thread, using thread methods. So i create an array called myThread, which is of type TestThread(my class) and can then use the method start(), as the class extends thread.

Hope this helps.




  Re: Array and Threads  ericwi at 09:37 on Thursday, January 25, 2007
 

Thank you mmarab,

That solves the error message. I tried to implement Runnable and it works too.


  Re: Array and Threads  mmarab at 09:39 on Thursday, January 25, 2007
 

Excellent, glad that it helped!








CodeToad Experts

Can't find the answer?
Our Site experts are answering questions for free in the CodeToad forums








Recent Forum Threads
•  Re: Files
•  Re: Manipulating large Images.
•  Re: Replacing Class definition comments in java files
•  Re: Multiplication Problem
•  Re: Overlaying frames
•  Re: Array and Threads
•  Re: Compiling JAVA code in .NET?
•  Re: Perl for Executing Shell Script
•  Re: .net


Recent Articles
ASP GetTempName
Decode and Encode UTF-8
ASP GetFile
ASP FolderExists
ASP FileExists
ASP OpenTextFile
ASP FilesystemObject
ASP CreateFolder
ASP CreateTextFile
Javascript Get Selected Text


© Copyright codetoad.com 2001-2007