Sunday, July 19, 2009

Puzzle 43 – Run a Thread.

Language – Java | Type – Concept | Last date 22-Jul-2009 9:00 p.m. IST | Points 3

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.


12 comments:

  1. class 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();
    }
    }

    ReplyDelete
  2. package 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();
    }
    }

    ReplyDelete
  3. 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


    package 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();
    }
    }

    ReplyDelete
  4. add "extends Thread " & rename run() to run_()
    15+1=16 characters

    ReplyDelete
  5. I dont know this is valid or not, What I have done is
    I 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();
    }
    }

    ReplyDelete
  6. 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)

    class 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();
      }
    }

    ReplyDelete
  7. You gave to many clues :

    class 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();
    }
    }

    ReplyDelete
  8. All that we need - just to extend Thread and rename run method - 16 characters

    public class MultiThreader extends Thread implements Runnable {
    public void _run() {
    System.out.println("STOP ME IF YOU CAN");
    }

    ReplyDelete
  9. Add a
    {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 :-)

    ReplyDelete
  10. 10 additional characters

    package 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();
    }
    }

    ReplyDelete
  11. I added 16 characters - "extends Thread " and the number "2" appended to the run method.

    class 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();
    }
    }

    ReplyDelete
  12. //add empty start() method.
    public void start(){}

    ReplyDelete

Solution for this question?