High Fashioners Issue # 2 | Page 295

J. E. N. I.
Thread Methods public void start() Menyebabkan eksekusi dari thread berlangsung dengan cara memanggil method run.
Tabel 1.2.3: Method-method dari Thread
9.3.4 Sebuah contoh thread
Contoh dari thread pertama Anda adalah sebuah counter yang sederhana.
import javax. swing.*; import java. awt.*;
class CountDownGUI extends JFrame { JLabel label; CountDownGUI( String title) { super( title); label = new JLabel(" Start count!"); setDefaultCloseOperation( JFrame. EXIT _ ON _ CLOSE); getContentPane(). add( new Panel(), BorderLayout. WEST); getContentPane(). add( label); setSize( 300,300); setVisible( true);
} void startCount() { try { for( int i = 10; i > 0; i--) { Thread. sleep( 1000); label. setText( i + "");
} Thread. sleep( 1000); label. setText(" Count down complete."); Thread. sleep( 1000); } catch( InterruptedException ie) { } label. setText( Thread. currentThread(). toString());
} public static void main( String args []) { CountDownGUI cdg = new CountDownGUI(" Count down GUI "); cdg. startCount();
}
}
9.4 Membuat Threads
Sebuah thread dapat diciptakan dengan cara menurunkan( extend) class Thread atau dengan mengimplementasikan sebuah interface Runnable.
Pengenalan Pemrograman 2 5