Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating Multiple Threaded Loops

Sorry if this a bit of a basic question but I've been thinking about doing multiple sprite loops and for the first time I tried to create two threads in main, both with while(true) loops. My intention: to have two threads looping simultaneously. However when I run the program it seems to interrupt the flow of execution and the second loop doesn't getting executed in a new thread but just stops with the program stuck on the first endless while() loop of a thread. I think it is still just executing the main thread rather than starting a new one and then continuing on.

I've tried it two ways:

Once with Threads:

public class Zzz {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        r1 r = new r1();
        r2 a = new r2();
        r.start();
        a.start();
    }
}

public class r1 extends Thread {

    @Override
    public void start() {
        while(true) {
            System.out.println("r1");
            try {
                this.sleep(100);
            } catch (Exception ex) {

            }
        }
    }

}

public class r2 extends Thread {

    @Override
    public void start() {
        while(true) {
            System.out.println("r2");
                        try {
                this.sleep(100);
            } catch (Exception ex) {

            }
        }
    }

}

And once with Runnable:

public class Zzz {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        r1 r = new r1();
        r2 a = new r2();
        r.run();
        a.run();
    }
}

public class r1 implements Runnable {

    @Override
    public void run() {
        while(true) {
            System.out.println("r1");
            try {
                Thread.sleep(100);
            } catch (Exception ex) {

            }
        }
    }

}

public class r2 implements Runnable {

    @Override
    public void run() {
        while(true) {
            System.out.println("r2");
                        try {
                Thread.sleep(100);
            } catch (Exception ex) {

            }
        }
    }

}

But to no avail. It always gets stuck at R1. Any ideas anyone? I've googled and looked around about threads and I can't find this covered anywhere.

like image 247
user1060899 Avatar asked Jan 20 '26 23:01

user1060899


1 Answers

You need to override run method & in case of runnable you need to create instance of Thread

public class MyThread extends Thread{
    @Override
    public void run() {
        while(true) {
            System.out.println("My Thread running");
        }

}

ánd for the case of Runnable

class MyRunnable implements Runnable{

             public void run(){
                         System.out.println("I am executing by Thread: " + Thread.currentThread().getName());
             }
 }

and

  Thread mythread = new MyThread();
  mythread.setName("T1");
  Thread myrunnable = new Thread(new MyRunnable());
  myrunnable.start();
like image 184
jmj Avatar answered Jan 23 '26 12:01

jmj



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!