Self-inflicted killing utilityConcurrent event counter/averager utility classesSelf-implemented C++ exception classIs this file I/O utility efficient or is there a lot of fat?Temporary file utility class in PythonSelf-written Mutex for 2+ ThreadsSelf-stopping started and bound Android serviceImplementation of stackPerformance issue in generic sql utilityBinary data utility classImplementation of unix utility tail in C
how to say 'nerd' or 'geek' in french?
What is the design rationale for having armor and magic penetration mechanics?
Find number 8 with the least number of tries
Email suggestion for a rogue email from my manager
Car as a good investment
2 Guards, 3 Keys, 2 Locks
Is the tap water in France safe to drink?
TFT LCD Tear-down Silver Rectangle Question
To what extent would using the initiative system from Edge of the Empire in 5e break the game?
Can digital computers understand infinity?
Why is Trump releasing or not of his taxes such a big deal?
Milk instead of water in bread
When did MCMC become commonplace?
Wanted references to the Phillip K Dick Total Recall (1990) paradox
In the twin paradox does the returning twin also come back permanently length contracted flatter than the twin on earth?
Charges from Dollar General have never shown up on my debit card - how to resolve?
How can I communicate feelings to players without impacting their agency?
Why do adjectives come before nouns in English?
What does the British parliament hope to achieve by requesting a third Brexit extension?
What Apple System Monitor/Integer BASIC ROM features were removed in the Applesoft II/Autostart ROM?
Why should be velocity through the nozzle throat be sonic?
Remove last letter 4 times, get a real word each time, starting word is a car model
What does this text mean with capitalized letters?
Is there a way to make editing enjoyable?
Self-inflicted killing utility
Concurrent event counter/averager utility classesSelf-implemented C++ exception classIs this file I/O utility efficient or is there a lot of fat?Temporary file utility class in PythonSelf-written Mutex for 2+ ThreadsSelf-stopping started and bound Android serviceImplementation of stackPerformance issue in generic sql utilityBinary data utility classImplementation of unix utility tail in C
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty
margin-bottom:0;
$begingroup$
If you need to kill the same process that you're in (for testing purposes or whatever) this code will do it.
A definitive, quick, unmerciful dead of the current Java program/app.
Not a System.exit(0) or a graceful dead.
A headshot to the running JVM
public class KillMe
private static final String CMD_WINDOWS = "taskkill /F /PID %d";
private static final String CMD_LINUX = "kill -9 %d";
public static void tryNow()
Thread t = new Thread( () ->
try
now();
catch (CantKillMeException e)
//TODO whatever
e.printStackTrace();
);
t.setName("killmethread");
t.setDaemon(true);
t.start();
public static void now() throws CantKillMeException
int pid = obtainPid();
String command = getCommand(pid);
executeCommand(command);
static int obtainPid() throws CantKillMeException NoSuchMethodException
// https://stackoverflow.com/questions/9573696/kill-a-process-based-on-pid-in-java
static String getCommand(int pid)
String command = isWindows() ? CMD_WINDOWS : CMD_LINUX;
return String.format(command,pid);
// https://stackoverflow.com/questions/14288185/detecting-windows-or-linux
static boolean isWindows()
String osname = System.getProperty("os.name").toLowerCase();
return osname.contains("win");
static void executeCommand(String command) throws CantKillMeException InterruptedException e)
throw new CantKillMeException("Cant execute command " + command, e);
public static class CantKillMeException extends Exception
CantKillMeException(String message, Throwable cause)
super(message, cause);
Will work on Linux. Windows should work (not tested). There's room for improvement, for sure.
java multithreading io exception
$endgroup$
add a comment
|
$begingroup$
If you need to kill the same process that you're in (for testing purposes or whatever) this code will do it.
A definitive, quick, unmerciful dead of the current Java program/app.
Not a System.exit(0) or a graceful dead.
A headshot to the running JVM
public class KillMe
private static final String CMD_WINDOWS = "taskkill /F /PID %d";
private static final String CMD_LINUX = "kill -9 %d";
public static void tryNow()
Thread t = new Thread( () ->
try
now();
catch (CantKillMeException e)
//TODO whatever
e.printStackTrace();
);
t.setName("killmethread");
t.setDaemon(true);
t.start();
public static void now() throws CantKillMeException
int pid = obtainPid();
String command = getCommand(pid);
executeCommand(command);
static int obtainPid() throws CantKillMeException NoSuchMethodException
// https://stackoverflow.com/questions/9573696/kill-a-process-based-on-pid-in-java
static String getCommand(int pid)
String command = isWindows() ? CMD_WINDOWS : CMD_LINUX;
return String.format(command,pid);
// https://stackoverflow.com/questions/14288185/detecting-windows-or-linux
static boolean isWindows()
String osname = System.getProperty("os.name").toLowerCase();
return osname.contains("win");
static void executeCommand(String command) throws CantKillMeException InterruptedException e)
throw new CantKillMeException("Cant execute command " + command, e);
public static class CantKillMeException extends Exception
CantKillMeException(String message, Throwable cause)
super(message, cause);
Will work on Linux. Windows should work (not tested). There's room for improvement, for sure.
java multithreading io exception
$endgroup$
1
$begingroup$
Indentation is a bit off, is it a copy paste issue?
$endgroup$
– bhathiya-perera
6 hours ago
$begingroup$
What exactly do you want reviewed?
$endgroup$
– JL2210
2 hours ago
add a comment
|
$begingroup$
If you need to kill the same process that you're in (for testing purposes or whatever) this code will do it.
A definitive, quick, unmerciful dead of the current Java program/app.
Not a System.exit(0) or a graceful dead.
A headshot to the running JVM
public class KillMe
private static final String CMD_WINDOWS = "taskkill /F /PID %d";
private static final String CMD_LINUX = "kill -9 %d";
public static void tryNow()
Thread t = new Thread( () ->
try
now();
catch (CantKillMeException e)
//TODO whatever
e.printStackTrace();
);
t.setName("killmethread");
t.setDaemon(true);
t.start();
public static void now() throws CantKillMeException
int pid = obtainPid();
String command = getCommand(pid);
executeCommand(command);
static int obtainPid() throws CantKillMeException NoSuchMethodException
// https://stackoverflow.com/questions/9573696/kill-a-process-based-on-pid-in-java
static String getCommand(int pid)
String command = isWindows() ? CMD_WINDOWS : CMD_LINUX;
return String.format(command,pid);
// https://stackoverflow.com/questions/14288185/detecting-windows-or-linux
static boolean isWindows()
String osname = System.getProperty("os.name").toLowerCase();
return osname.contains("win");
static void executeCommand(String command) throws CantKillMeException InterruptedException e)
throw new CantKillMeException("Cant execute command " + command, e);
public static class CantKillMeException extends Exception
CantKillMeException(String message, Throwable cause)
super(message, cause);
Will work on Linux. Windows should work (not tested). There's room for improvement, for sure.
java multithreading io exception
$endgroup$
If you need to kill the same process that you're in (for testing purposes or whatever) this code will do it.
A definitive, quick, unmerciful dead of the current Java program/app.
Not a System.exit(0) or a graceful dead.
A headshot to the running JVM
public class KillMe
private static final String CMD_WINDOWS = "taskkill /F /PID %d";
private static final String CMD_LINUX = "kill -9 %d";
public static void tryNow()
Thread t = new Thread( () ->
try
now();
catch (CantKillMeException e)
//TODO whatever
e.printStackTrace();
);
t.setName("killmethread");
t.setDaemon(true);
t.start();
public static void now() throws CantKillMeException
int pid = obtainPid();
String command = getCommand(pid);
executeCommand(command);
static int obtainPid() throws CantKillMeException NoSuchMethodException
// https://stackoverflow.com/questions/9573696/kill-a-process-based-on-pid-in-java
static String getCommand(int pid)
String command = isWindows() ? CMD_WINDOWS : CMD_LINUX;
return String.format(command,pid);
// https://stackoverflow.com/questions/14288185/detecting-windows-or-linux
static boolean isWindows()
String osname = System.getProperty("os.name").toLowerCase();
return osname.contains("win");
static void executeCommand(String command) throws CantKillMeException InterruptedException e)
throw new CantKillMeException("Cant execute command " + command, e);
public static class CantKillMeException extends Exception
CantKillMeException(String message, Throwable cause)
super(message, cause);
Will work on Linux. Windows should work (not tested). There's room for improvement, for sure.
java multithreading io exception
java multithreading io exception
edited 2 hours ago
rolfl♦
92.1k15 gold badges199 silver badges402 bronze badges
92.1k15 gold badges199 silver badges402 bronze badges
asked 8 hours ago
Oscar Besga PanelOscar Besga Panel
663 bronze badges
663 bronze badges
1
$begingroup$
Indentation is a bit off, is it a copy paste issue?
$endgroup$
– bhathiya-perera
6 hours ago
$begingroup$
What exactly do you want reviewed?
$endgroup$
– JL2210
2 hours ago
add a comment
|
1
$begingroup$
Indentation is a bit off, is it a copy paste issue?
$endgroup$
– bhathiya-perera
6 hours ago
$begingroup$
What exactly do you want reviewed?
$endgroup$
– JL2210
2 hours ago
1
1
$begingroup$
Indentation is a bit off, is it a copy paste issue?
$endgroup$
– bhathiya-perera
6 hours ago
$begingroup$
Indentation is a bit off, is it a copy paste issue?
$endgroup$
– bhathiya-perera
6 hours ago
$begingroup$
What exactly do you want reviewed?
$endgroup$
– JL2210
2 hours ago
$begingroup$
What exactly do you want reviewed?
$endgroup$
– JL2210
2 hours ago
add a comment
|
1 Answer
1
active
oldest
votes
$begingroup$
Interesting little plot twist in a Java program.
First up, tryNow() is dead code, it's not used, get rid of it, it makes the rest of the class harder to understand by putting red-herrings in the code.
If it is really used somewhere else in your code (it is public static), then it should be moved out of this class in to a more usefully named location.... and it has other issues too, which I will ignore... except for .... never extend Thread directly, use Runnable ....
Next up, you should ensure you are using Java 9.x or newer, and then use the more convenient ProcessHandle API for getting your current processID
This reduces the code:
static int obtainPid() throws CantKillMeException
NoSuchFieldException e)
throw new CantKillMeException("Cant obtain pid", e);
to just:
static int obtainPid() throws CantKillMeException
try
return ProcessHandle.current().pid();
catch (SecurityException e)
throw new CantKillMeException("Cant obtain pid", e);
Although you don't want to do a System.exit(0) I would still plan it as a backup...
You have
static void executeCommand(String command) throws CantKillMeException
try
Process process = Runtime.getRuntime().exec(command);
process.waitFor();
catch (java.io.IOException
which anticipates exceptions on something that should terminate the process....
I would instead log the exception (actually, catch any Throwable), and exit.... within a finally-block - I would do that in the now() method:
public static void now() throws CantKillMeException
try
int pid = obtainPid();
String command = getCommand(pid);
executeCommand(command);
catch (Throwable t)
System.out.println(t);
finally
System.exit(1);
You can choose a different (improved) logging mechanism.
$endgroup$
add a comment
|
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "196"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/4.0/"u003ecc by-sa 4.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f230373%2fself-inflicted-killing-utility%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
$begingroup$
Interesting little plot twist in a Java program.
First up, tryNow() is dead code, it's not used, get rid of it, it makes the rest of the class harder to understand by putting red-herrings in the code.
If it is really used somewhere else in your code (it is public static), then it should be moved out of this class in to a more usefully named location.... and it has other issues too, which I will ignore... except for .... never extend Thread directly, use Runnable ....
Next up, you should ensure you are using Java 9.x or newer, and then use the more convenient ProcessHandle API for getting your current processID
This reduces the code:
static int obtainPid() throws CantKillMeException
NoSuchFieldException e)
throw new CantKillMeException("Cant obtain pid", e);
to just:
static int obtainPid() throws CantKillMeException
try
return ProcessHandle.current().pid();
catch (SecurityException e)
throw new CantKillMeException("Cant obtain pid", e);
Although you don't want to do a System.exit(0) I would still plan it as a backup...
You have
static void executeCommand(String command) throws CantKillMeException
try
Process process = Runtime.getRuntime().exec(command);
process.waitFor();
catch (java.io.IOException
which anticipates exceptions on something that should terminate the process....
I would instead log the exception (actually, catch any Throwable), and exit.... within a finally-block - I would do that in the now() method:
public static void now() throws CantKillMeException
try
int pid = obtainPid();
String command = getCommand(pid);
executeCommand(command);
catch (Throwable t)
System.out.println(t);
finally
System.exit(1);
You can choose a different (improved) logging mechanism.
$endgroup$
add a comment
|
$begingroup$
Interesting little plot twist in a Java program.
First up, tryNow() is dead code, it's not used, get rid of it, it makes the rest of the class harder to understand by putting red-herrings in the code.
If it is really used somewhere else in your code (it is public static), then it should be moved out of this class in to a more usefully named location.... and it has other issues too, which I will ignore... except for .... never extend Thread directly, use Runnable ....
Next up, you should ensure you are using Java 9.x or newer, and then use the more convenient ProcessHandle API for getting your current processID
This reduces the code:
static int obtainPid() throws CantKillMeException
NoSuchFieldException e)
throw new CantKillMeException("Cant obtain pid", e);
to just:
static int obtainPid() throws CantKillMeException
try
return ProcessHandle.current().pid();
catch (SecurityException e)
throw new CantKillMeException("Cant obtain pid", e);
Although you don't want to do a System.exit(0) I would still plan it as a backup...
You have
static void executeCommand(String command) throws CantKillMeException
try
Process process = Runtime.getRuntime().exec(command);
process.waitFor();
catch (java.io.IOException
which anticipates exceptions on something that should terminate the process....
I would instead log the exception (actually, catch any Throwable), and exit.... within a finally-block - I would do that in the now() method:
public static void now() throws CantKillMeException
try
int pid = obtainPid();
String command = getCommand(pid);
executeCommand(command);
catch (Throwable t)
System.out.println(t);
finally
System.exit(1);
You can choose a different (improved) logging mechanism.
$endgroup$
add a comment
|
$begingroup$
Interesting little plot twist in a Java program.
First up, tryNow() is dead code, it's not used, get rid of it, it makes the rest of the class harder to understand by putting red-herrings in the code.
If it is really used somewhere else in your code (it is public static), then it should be moved out of this class in to a more usefully named location.... and it has other issues too, which I will ignore... except for .... never extend Thread directly, use Runnable ....
Next up, you should ensure you are using Java 9.x or newer, and then use the more convenient ProcessHandle API for getting your current processID
This reduces the code:
static int obtainPid() throws CantKillMeException
NoSuchFieldException e)
throw new CantKillMeException("Cant obtain pid", e);
to just:
static int obtainPid() throws CantKillMeException
try
return ProcessHandle.current().pid();
catch (SecurityException e)
throw new CantKillMeException("Cant obtain pid", e);
Although you don't want to do a System.exit(0) I would still plan it as a backup...
You have
static void executeCommand(String command) throws CantKillMeException
try
Process process = Runtime.getRuntime().exec(command);
process.waitFor();
catch (java.io.IOException
which anticipates exceptions on something that should terminate the process....
I would instead log the exception (actually, catch any Throwable), and exit.... within a finally-block - I would do that in the now() method:
public static void now() throws CantKillMeException
try
int pid = obtainPid();
String command = getCommand(pid);
executeCommand(command);
catch (Throwable t)
System.out.println(t);
finally
System.exit(1);
You can choose a different (improved) logging mechanism.
$endgroup$
Interesting little plot twist in a Java program.
First up, tryNow() is dead code, it's not used, get rid of it, it makes the rest of the class harder to understand by putting red-herrings in the code.
If it is really used somewhere else in your code (it is public static), then it should be moved out of this class in to a more usefully named location.... and it has other issues too, which I will ignore... except for .... never extend Thread directly, use Runnable ....
Next up, you should ensure you are using Java 9.x or newer, and then use the more convenient ProcessHandle API for getting your current processID
This reduces the code:
static int obtainPid() throws CantKillMeException
NoSuchFieldException e)
throw new CantKillMeException("Cant obtain pid", e);
to just:
static int obtainPid() throws CantKillMeException
try
return ProcessHandle.current().pid();
catch (SecurityException e)
throw new CantKillMeException("Cant obtain pid", e);
Although you don't want to do a System.exit(0) I would still plan it as a backup...
You have
static void executeCommand(String command) throws CantKillMeException
try
Process process = Runtime.getRuntime().exec(command);
process.waitFor();
catch (java.io.IOException
which anticipates exceptions on something that should terminate the process....
I would instead log the exception (actually, catch any Throwable), and exit.... within a finally-block - I would do that in the now() method:
public static void now() throws CantKillMeException
try
int pid = obtainPid();
String command = getCommand(pid);
executeCommand(command);
catch (Throwable t)
System.out.println(t);
finally
System.exit(1);
You can choose a different (improved) logging mechanism.
answered 3 hours ago
rolfl♦rolfl
92.1k15 gold badges199 silver badges402 bronze badges
92.1k15 gold badges199 silver badges402 bronze badges
add a comment
|
add a comment
|
Thanks for contributing an answer to Code Review Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
Use MathJax to format equations. MathJax reference.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f230373%2fself-inflicted-killing-utility%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
$begingroup$
Indentation is a bit off, is it a copy paste issue?
$endgroup$
– bhathiya-perera
6 hours ago
$begingroup$
What exactly do you want reviewed?
$endgroup$
– JL2210
2 hours ago