Contrary to what the title says – this puzzle is about stopping a rouge thread form running.
The challenge is pretty simple – all you need to do is stop the code from printing “STOP ME IF YOU CAN”
package com.twisters;
class MultiThreader implements Runnable{
public void run(){
System.out.println( "STOP ME IF YOU CAN");
}
public static void main(String[] args) {
MultiThreader m = new MultiThreader();
Thread t = new Thread(m);
t.start();
}
}
Rules:
No code may be deleted or commented – but you folks already know this!
No changes to or in the main method at all.
No code may be added to the body of the run() method!
One way to solve this is to rename the run() method to _run() and write an empty run() method. This needs a minimum 20 characters including the necessary white space. You may use a maximum of 18 characters including any white spaces. (Growing pretty obsessive with this 18 character limit – ain’t I)
Pretty simple puzzle to be honest!!
Got an answer? Leave your answer here.
class MultiThreader extends Thread implements Runnable {
ReplyDeletepublic void _run() {
System.out.println("STOP ME IF YOU CAN");
}
public static void main(String[] args) {
MultiThreader m = new MultiThreader();
Thread t = new Thread(m);
t.start();
}
}
package com.twisters;
ReplyDeleteclass MultiThreader implements Runnable
{
{
System.exit(0);
}
public void run()
{
System.out.println( "STOP ME IF YOU CAN");
}
public static void main(String[] args) {
MultiThreader m = new MultiThreader();
Thread t = new Thread(m);
t.start();
}
}
The Solution for the above puzzle can be done through Instance Initializer Block by including System.exit(0) in that. If we use static initializer we get more than 18 characters, so the above can be acheived by Instance Initializer block
ReplyDeletepackage com.twisters;
class MultiThreader implements Runnable{
{
System.exit(0);
}
public void run()
{
System.out.println( "STOP ME IF YOU CAN");
}
public static void main(String[] args) {
MultiThreader m = new MultiThreader();
Thread t = new Thread(m);
t.start();
}
}
add "extends Thread " & rename run() to run_()
ReplyDelete15+1=16 characters
I dont know this is valid or not, What I have done is
ReplyDeleteI have ended the run definition by putting a ending brace after staring brace and then for correcting code added method void m() which takes the body of run.
package com.twisters;
class MultiThreader implements Runnable{
public void run(){}
void m(){
System.out.println( "STOP ME IF YOU CAN");
}
public static void main(String[] args) {
MultiThreader m = new MultiThreader();
Thread t = new Thread(m);
t.start();
}
}
First solution: rename run() to _run() and extend a class that implements Runnable with a run method that doesn't actually do anything. (16 characters added)
ReplyDeleteclass MultiThreader extends Thread implements Runnable {
public void _run() {
System.out.println("STOP ME IF YOU CAN");
}
public static void main(String[] args) {
MultiThreader m = new MultiThreader();
Thread t = new Thread(m);
t.start();
}
}
Second solution: call System.exit(0) in an initializer block of MultiThreader. (16 characters added)
class MultiThreader implements Runnable {
{System.exit(0);}
public void run() {
System.out.println("STOP ME IF YOU CAN");
}
public static void main(String[] args) {
MultiThreader m = new MultiThreader();
Thread t = new Thread(m);
t.start();
}
}
You gave to many clues :
ReplyDeleteclass MultiThreader extends Thread implements Runnable {
public void _run() {
System.out.println("STOP ME IF YOU CAN");
}
public static void main(String[] args) {
MultiThreader m = new MultiThreader();
Thread t = new Thread(m);
t.start();
}
}
All that we need - just to extend Thread and rename run method - 16 characters
ReplyDeletepublic class MultiThreader extends Thread implements Runnable {
public void _run() {
System.out.println("STOP ME IF YOU CAN");
}
Add a
ReplyDelete{notify();}
To the MultiThreader class and it will not run because the initializer throws an exception, this makes for 11 changes/added characters.
'will try to find a more compact solution :-)
10 additional characters
ReplyDeletepackage com.twisters;
class MultiThreader implements Runnable{
public void run(){
System.out.println( "STOP ME IF YOU CAN");
}
int a=4/0;
public static void main(String[] args) {
MultiThreader m = new MultiThreader();
Thread t = new Thread(m);
t.start();
}
}
I added 16 characters - "extends Thread " and the number "2" appended to the run method.
ReplyDeleteclass MultiThreader extends Thread implements Runnable{
public void run2(){
System.out.println( "STOP ME IF YOU CAN");
}
public static void main(String[] args) {
MultiThreader m = new MultiThreader();
Thread t = new Thread(m);
t.start();
}
}
//add empty start() method.
ReplyDeletepublic void start(){}