views:

148

answers:

3

Hi,

my code is as follows

public void incomingMessageThread() throws FileNotFoundException, IOException
{
    new Thread()
    {

        BuildData a = new BuildData();
        for(int i = 0; i<100; i++)
        {
            a.parseDataFile("_"+i+"/outgoingMessages");
        }

    }.start();

}

I get told its an illegal start of line. If I run the code outside a thread it works fine. Any ideas whats wrong?

A: 

Thread is a class not a function ( which is the closet your syntax above resembles )

your code should be

class MyThread : public Thread {
 public void run() {
    // code
 }
}

Thread t = new MyThread();
t.run()
George
should be t.start() not t.run()
Glen
He's trying to use an anonymous inner class.
SLaks
+11  A: 

You are using statements inside of a class and outside of a method.

From the javadoc for Thread.run: "Subclasses of Thread should override this method."

public void incomingMessageThread() throws FileNotFoundException, IOException
{
    new Thread()
    {
        public void run()
        {
            BuildData a = new BuildData();
            for(int i = 0; i<100; i++)
            {
                a.parseDataFile("_"+i+"/outgoingMessages");
            }
        }

    }.start();

}
Alex B
+4  A: 

you should have written something like this (implement void run() )

public void incomingMessageThread() throws FileNotFoundException, IOException
{
Thread t= new Thread()
    {
    public void run()
        {
        BuildData a = new BuildData();
        for(int i = 0; i<100; i++)
          {
            a.parseDataFile("_"+i+"/outgoingMessages");
           }
        }
    };
t.start();
}
Pierre