I have a thread in which data is scanning from standard input stream as:
1. Scanner scan = new Scanner(System.in);
2. while(true)
3. {
4. String str = scan.nextLine();
5. System.out.println(str);
6. }
I want to stop this thread by another thread. I heard it is not good to use Thread.stop(). Using some boolean flag also will not solve it as the pointer (program execution step) will be always at line 4. String str = scan.nextLine();
Is there any way that I can stop the thread?
I think this article will help you, it comes with a code example that solves the problem.
You could either use that code directly, or write a new closable InputStream class, wrapping up the logic described in this article.