← Back to feed
Renewal·서른의 생활코딩

ex4) Java Thread - Runnable Interface

NS
normalstory
cover image


 

/*
 Runnable :run()
 -> RunnableTest :redefine run()  (+ <-automatically object:wait,notify...+run()   (+ttreference variable))
 
 => additionally a separate Thread inherit. ->t
 => tt t somewhere in "address value"must be referenced.
 
 => execution process: t->ttaddress value->tt->run()
 **/

 

public class RunnableTest implements Runnable {
 
 public void printNumber() {// method that prints numbers from 0 to 1 on screen
  for( int i=1 ; i<=20 ; i++ ) {
   System.out.println( "number = " + i );
  }
 }
  
 public void run() {// overriding run() of the Runnable interface
  printNumber();
 }
 
 public static void main( String[] args ) {
  ThreadTest tt = new ThreadTest();// create object
  
  Thread t = new Thread( tt );// create Thread class object 
  t.start();// start the Thread
  System.out.println( "--------> main thread end" );
 }
}

This English version was translated by Claude.

친절한 찰쓰씨
Written by
친절한 찰쓰씨

Pleasant Charles — UI/UX researcher at AIT. Keeping notes on design, planning, and slow days here since 2010.