1

This thread does not execute when I run the program. I'm wondering if there's something wrong with the code.

public static void writeToFileAsync(final String saveState, final String fileName)  {    
    new Thread() {
        public void run() {
            try {
                writeToFile(saveState, fileName);
            } catch (IOException ex) {

            }
            start();
        }
    };
}

Also, why does NetBeans want me to put that semicolon next to the second curly brace after the start() call?

Zabuzard
  • 25,064
  • 8
  • 58
  • 82
Mike2
  • 19
  • 1
  • You have to call `start` after you created the Thread-Object. Look at some thread-examples on the Internet! – Thomas Böhm Oct 30 '17 at 20:07
  • You're not starting your thread. – shmosel Oct 30 '17 at 20:07
  • Why should it run? You need to call the `start` method on the `Thread`. Take a look at the documentation [Thread#start](https://docs.oracle.com/javase/8/docs/api/java/lang/Thread.html#start--) – Zabuzard Oct 30 '17 at 20:07
  • 2
    *Also, why does NetBeans want me to put that semicolon next to the second curly brace after the start() call?* Because `new Thread() { ... };` is a statement. – shmosel Oct 30 '17 at 20:08

3 Answers3

3

Start a thread

Your thread will only start if you call the start method explicitly. Here is the documentation Thread#start. The start method will then internally invoke the run method of your Thread.

Your code could then look like this:

public static void writeToFileAsync(final String saveState, final String fileName)  {
    // Create the thread
    Thread fileWriter = new Thread() {
        @Override
        public void run() {
            try {
                writeToFile(saveState, fileName);
            } catch (IOException ex) {
                // Do nothing
            }
        }
    };

    // Start the thread
    fileWriter.start();
}

And you probably want to remove the start(); call inside your run method.


Semicolon

You need the ; after the Thread creation because you are using an assignment:

Thread fileWriter = new Thread() { ... };

The concept you are using here is called anonymous class. Basically it is the same as if creating a new class like:

public class FileWriter extends Thread  {
    @Override
    public void run() {
        ...
    }
}

And then using it like:

Thread fileWriter = new FileWriter();

However an important difference is that your anonymous class has access to your local variables (the scope of that method). And that it is anonymous, so it's like a small single-time usage class.

Zabuzard
  • 25,064
  • 8
  • 58
  • 82
1

Your call to the start method cannot be inside the body of your thread. You can do this:

new Thread() {
    public void run() {
        try {
            writeToFile(saveState, fileName);
        } catch (IOException ex) {

        }
    }
}.start(); // call the start method outside the body of you thread.

And about the semicolon, you are creating an Anonymous Class and that is its syntax:

Because an anonymous class definition is an expression, it must be part of a statement... (This explains why there is a semicolon after the closing brace.)

Juan Carlos Mendoza
  • 5,736
  • 7
  • 25
  • 50
0

A thread simply works this way. Below is a piece of code where a thread is created as an anonymous inner type where the run method is overrided. Then by calling the start method , it automatically called the overrided run method.

public class ThreadTest {

    Thread t = new Thread(){
        public void run() {
            System.out.println("thread is  running");
        };
    };

    public static void main(String[] args) {
        ThreadTest threadTest = new ThreadTest();
        threadTest.t.start();
    }
}
Juan Carlos Mendoza
  • 5,736
  • 7
  • 25
  • 50