Auf Thema antworten

hier mal ein Minimalbsp mit Konsolenausgabe:

Die endlos-Schleife schreibt alle 2 sec eine Ausgabe auf die Konsole, alle 10 sec kommt ein anderer Thread und schreibt halt was anderes....


[code=Java]public class DelayExample {

    public static void main(String[] args) {

        new DelayExample();


    }


    private int i = 1;


    public DelayExample() {


        new Thread(new Runnable() {


            @Override

            public void run() {

                while (true) {

                    try {

                        Thread.sleep(10000);

                    } catch (InterruptedException e) {

                        // TODO Auto-generated catch block

                        e.printStackTrace();

                    }

                    System.out

                    .println("\tunterbricht die Endlosschleife alle 10 sec");

                }

            }

        }).start();

        endlosSchleife("Ich bin eine Endlosschleife");

    }


    private void endlosSchleife(String s) {

        while (true) {

            System.out.println(s + " im " + i + ". Durchgang");

            i++;

            try {

                Thread.sleep(2000);

            } catch (InterruptedException e) {

                e.printStackTrace();

            }

        }

    }

}

[/code]



Oben