Running Thread's in Java Throws an Exception [duplicate] - java

This question already has answers here:
java.lang.IllegalThreadStateException
(6 answers)
Closed 2 years ago.
I am trying to create a game and have come across a bizarre issue with the way I choose to run my Threads
The way I have it set up is that each pertinent class has a Thread Object, who's run() function runs the code once. Each is called once every frame.
However whenever I try to run the code I get a java.lang.IllegalThreadStateException and I have no clue why.
Here's an adaptation of the problematic code:
public class Test {
public static void main(String[] args) {
while(true) {
ThreadA.run(args);
ThreadB.run(args);
}
}
private static class ThreadA {
private static Thread thread = new Thread() {
#Override
public void run() {
System.out.println("A");
}
};
public static void run(String[] args) {
thread.start();
}
}
private static class ThreadB {
private static Thread thread = new Thread() {
#Override
public void run() {
System.out.println("B");
}
};
public static void run(String[] args) {
thread.start();
}
}
}

The problem is here:
while(true) {
ThreadA.run(args);
ThreadB.run(args);
}
This will loop infinitely. It is never legal to start a thread more than once - even if it has completed. thread.isAlive() won't help you here - if you want to run it again, make a new instance.

You're calling Thread#start over and over. It's throwing IllegalStateException because the threads internal status must be equal to 0 for it to start. Anything other than 0 means it has run, is running, or is disposed of.

As per JavaDoc:
It is never legal to start a thread more than once. In particular, a
thread may not be restarted once it has completed execution.
If you intend to run the threads infinitely, you can do it as
public class Test {
public static void main(String[] args) {
ThreadA.run(args);
ThreadB.run(args);
}
private static class ThreadA {
private static Thread thread = new Thread() {
#Override
public void run() {
while (true) {
System.out.println("A");
}
}
};
public static void run(String[] args) {
thread.start();
}
}
private static class ThreadB {
private static Thread thread = new Thread() {
#Override
public void run() {
while (true) {
System.out.println("B");
}
}
};
public static void run(String[] args) {
thread.start();
}
}
}

Related

Setting a static variable in a run () method in java

I have a static variable which I would like to set in a run() method. I have the following:
public class Test{
public static int temp;
public static void main(String [] args)
{
update();
System.out.println("This is the content of temp"+temp);
}
public static void update()
{
(new Thread() {
#Override
public void run() {
// do some stuff
Test.temp=15;
}}).start();
}
I would like the content of temp to be updated to 15; but when I print it in the main function, it shows 0. How can this be fixed?
Threads working concurrently so you should wait until your new thread finishes:
public class Test{
public static int temp;
public static void main(String [] args) {
update().join(); //we wait until new thread finishes
System.out.println("This is the content of temp"+temp);
}
public static Thread update() {
Thread t = new Thread() {
#Override
public void run() {
// do some stuff
Test.temp=15;
}
};
t.start();
return t;
}
You have to understand how Thread works.
I will show you two pieces of code here first is to understand, variables that are initialized inside the thread takes take time to update until the thread is finished.
public class Num {
public static int temp;
public static void main(String [] args) throws InterruptedException
{
update();
System.out.println("This is the content of temp"+Num.temp);//This will print before temp=15 is updated
}
public static void update()
{
(new Thread() {
#Override
public void run() {
// do some stuff
Num.temp=15;
System.out.println("Value of temp:"+Num.temp);//This statement prints after
}}).start();
}
}
It Prints the following:
This is the content of temp0
Value of temp:15
Second one shows, if you wait for a small amount time(Thread.sleep(10)) after the thread is executed, the value gets updated:
public class Num {
public static int temp;
public static void main(String [] args) throws InterruptedException
{
update();
Thread.sleep(10);
System.out.println("This is the content of temp"+Num.temp);//This will print correct value now
}
public static void update()
{
(new Thread() {
#Override
public void run() {
// do some stuff
Num.temp=15;
}}).start();
}
}
But here I would suggest the same method as Philip did. Just add throws InterruptedException in main function

Java threads behaving differnetly [duplicate]

This question already has answers here:
Program hangs if thread is created in static initializer block
(4 answers)
Closed 6 years ago.
This class does not initialize itself in the usual way, so it calls on the help of background thread.
From my understanding ,surely the program must print true ?
But If you ran the program, you found that it prints nothing; it just hangs.
public class Test {
private static boolean isInitialized = false;
static {
Thread t = new Thread(new Runnable() {
public void run() {
isInitialized = true;
}
});
t.start();
try {
t.join();
} catch (InterruptedException e) {
}
}
public static void main(String[] args) {
System.out.println(isInitialized);
}
}
Can some one please explain why this is happening.
"The static initializer for a class gets run when the class is first accessed, either to create an instance, or to access a static method or field." Static Initialization Blocks
I guess that this program to start needs to first initialize the class Test. And so it tries first to execute static block but that block never exits (since it cannot set static member isInitialized as it is not ready yet).
So Test class is never fully initialized and therefore main method is not even starting to be executed.
The solution for you could be moving the instruction of waiting for your initialization thread to finish - to the main method. So it does not block the Test class from being fully initailzed.
package com.company;
public class Test {
private static boolean isInitialized = false;
static Thread t = new Thread(new Runnable() {
public void run() {
isInitialized = true;
}
});
static {
t.start();
}
public static void main(String[] args) {
try {
t.join();
} catch (InterruptedException ignored) { }
System.out.println(isInitialized);
}
}

Java implements Runnable: Cannot resolve Symbol 'start'

I stripped everything out to see if I could narrow down my problem and figure it out but I'm embarrassed to say I'm still stumped. Can someone please explain to me why my IDE cannot resolve 'start'? I'm sure it's a stupid mistake :(
public class main {
Thread messageThread = new Thread(new MessageLoop());
messageThread.start();
class MessageLoop implements Runnable {
public void run(){
//Do stuff here
}
}
}
Instead of defining your class in your method body. Java variable names start with a lower case letter by convention. Comments start with // not \\. I think you wanted something like
public static void main(String[] args) {
Thread messageThread = new Thread(new MessageLoop());
messageThread.start();
}
static class MessageLoop implements Runnable {
public void run() {
// Do stuff here
}
}
Use following approaches.
Approach1
public static void main(String[] args) {
Thread MessageThread = new Thread(new MessageLoop());
MessageThread.start();
}
static class MessageLoop implements Runnable {
public void run(){
System.out.println("helllo");
}
}
Approach 2
public static void main(String[] args) {
Thread MessageThread2 = new Thread(new Runnable(){
public void run(){
System.out.println("helllo2");
}
});
MessageThread2.start();
}

Why doesn't run method call?

I've write the following example:
public class MyThread extends Thread{
MyThread(Runnable r){
super(r);
}
public void run(){
System.out.println("run");
}
}
public static void main(String[] args)
{
Thread t = new MyThread(new Runnable() {
#Override
public void run() {
System.out.println("rrrrrrrrrruuuuuuuuuuuun");
}
});
t.start(); //run
}
Why does run methdo defined in MyThread was called instead?
Because the default behavior of a thread constructed with a Runnable is to delegate to the runnable passed as argument to the constructor. But you overrode run() in the thread itself, so instead of delegating to the runnable, it executes the code inside the overridden run() method.
For the record, here's the default implementation of Thread.run(), that you overrode:
private Runnable target;
public void run() {
if (target != null) {
target.run();
}
}
Because you the MyThread.run is not override, but the Runnable.run is. Now if you look at your implementation of MyThread.run, the stored Runnable plays no part in it. In other words, it doesn't matter what kind of runnable you give with the constructor. You should use:
public static void main(String[] args)
{
Thread t = new MyThread() {
#Override
public void run() {
System.out.println("rrrrrrrrrruuuuuuuuuuuun");
}
});
t.start(); //run
}
As #BorisTheSpider notes, overriding a Thread is in general not good practice: a Thread has the responsibility to start a Thread and give control to a runnable. A better implementation would be:
public static void main(String[] args)
{
Thread t = new Thread(new MyThread() {
#Override
public void run() {
System.out.println("rrrrrrrrrruuuuuuuuuuuun");
}
}));
t.start(); //run
}

Thread Dependency Java

I have a main class which spawns a thread, let's call them MainClass and MyThread.
public class MainClass extends javax.swing.JFrame {
int sharedVariable;
MyThread threadInstance;
public MainClass (){
sharedVariable = 2;
threadInstance = new MyThread(this);
threadInstance.run();
}
public int getSharedVariable(){ return sharedVariable; }
public static void main(String[] args){
//begin main class
}
}
public class MyThread implements Runnable {
MainClass class;
public MyThread(MainClass main_class){
this.main_class= main_class;
}
#Override
public run(){
while(this.main_class is still active){
//grab status of sharedVariable and wait for x amount of time.
}
}
}
The problem is I do not know how to implement the while condition which checks if the MainClass instance is still alive and if it is, it has to use the this.main_class.getSharedVariable() to get the value of sharedVariable, then wait for x amount of time. MainClass has the main method .
I would recommend holding onto the Thread instance and then calling threadInstance.interrupt() right before the main(...) method exits.
Something like:
public static void main(String[] args){
MainClass mainClass = new MainClass();
try {
...
// do main stuff here
...
} finally {
mainClass.threadInstance.interrupt();
}
}
Then in your thread you'd do:
while(!Thread.currentThread().isInterrupted()){
...
}
You'd also want to handle InterruptedException correctly:
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// always a good pattern to re-interrupt the thread here
Thread.currentThread().interrupt();
// if we are interrupted quit
return;
}
Btw, it is very bad form to leak the instance of an object during construction to another thread:
new MyThread(this);
See here: Why shouldn't I use Thread.start() in the constructor of my class?
Also, you aren't starting a thread when you call threadInstance.run();. You are just running it in the current thread. You should use threadInstance.start() but not inside of the constructor like that.
You can use CountDownLatch which is very convenient for such tasks as waiting other threads to finish some activity (you can change Thread.sleep(...) argument in main to, say, 12000L and see what happens):
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
class OtherThread extends Thread {
private final CountDownLatch sharedLatch;
OtherThread(CountDownLatch sharedLatch) {
this.sharedLatch = sharedLatch;
}
#Override
public void run() {
boolean wokenByMain = false;
try {
wokenByMain = sharedLatch.await(10000L, TimeUnit.MILLISECONDS);
} catch (InterruptedException e) {
e.printStackTrace();
return; // or not return, whatever makes more sense in your case
}
System.out.println("heh: " + wokenByMain);
}
}
class SOSample {
public static void main(String[] args) throws InterruptedException {
CountDownLatch latch = new CountDownLatch(1);
OtherThread otherThread = new OtherThread(latch);
otherThread.start();
System.out.println("Scheduled other thread to be started");
Thread.sleep(1000L);
System.out.println("going to release other thread");
latch.countDown();
}
}
public class MainClass extends JFrame implements Runnable {
public static void main(String [] args) {
final Thread t=new Thread(new MainClass() {
public void run(){
//something
});
Thread t2=new Thread(new MyThread() {
public void run() {
while(t.isAlive) {
//something
}
}
});
}
}

Categories