Easy Coding
  Forum Wiki Tagging Projekte Karte RSS
» Start
» All Recent Changes
» Wiki Suche
» Wiki Hilfe

Coder How To's

Algorithmen Informationen

edit SideBar

Neue Wiki Eintrage finden Sie unter easy-coding.de/wiki.

Java-Threads

Threading.java
  1. class MyThread extends Thread {
  2.         private volatile Thread thread = this;
  3.  
  4.         /* Die Methode stop() kann nicht überschrieben werden da sie final ist */
  5.         public void myStop() {
  6.                 thread = null;
  7.         }
  8.  
  9.         public void run() {
  10.                 Thread thisThread = Thread.currentThread();
  11.                 int counter = 0;
  12.                 while (thread == thisThread) {
  13.                         System.out.print(currentThread()+": ");
  14.                         System.out.println(counter++);
  15.                 }
  16.         }
  17. }
  18.  
  19. public class Threading {
  20.         public static void main(String[] args) {
  21.                 MyThread thread = new MyThread();
  22.                 System.out.println("Starting thread...");
  23.                 thread.start();
  24.  
  25.                 try {
  26.                         Thread.sleep(20);
  27.                 } catch (InterruptedException e) {
  28.                         //Error
  29.                 }
  30.  
  31.                 thread.myStop();
  32.                 System.out.println("Thread has been stoped");
  33.                 System.out.print("Thread is alive: ");
  34.                 System.out.println(thread.isAlive());
  35.         }
  36. }

Ausgabe

  • Starting thread...
  • Thread[Thread-0,5,main]: 1
  • .
  • .
  • Thread[Thread-0,5,main]: 59
  • Thread has been stoped
  • Thread is alive:true
  • Thread[Thread-0,5,main]: 60

Verwandte Links

Zuletzt geändert am 01.10.2006 19:46 Uhr
  Impressum