I am learning how to use threads in Java. And I wrote a class that implements Runnable to simultaneously start another thread. The main thread handles listening on the serial port, where, when the second thread handles sending data to the same port.
public class MyNewThread implements Runnable { Thread t; MyNewThread() { t = new Thread (this, "Data Thread"); t.start(); } public void run() {
In the first thread, the second begins:
public class Main { public static void main(String[] args) throws Exception{ new MyNewThread();
This works, but my compiler notes a warning: it is dangerous to start a new thread in the constructor. Why is this?
The second part of this question is: how if I have a loop running in one thread (serial port listening thread) and I type the exit command in the second thread. How can I complete the first thread? Thank.
java multithreading terminate
Zac Apr 11 2018-11-11T00: 00Z
source share