Thread main 메서드의 Thread

2803 단어 thread

package scjp;

public class Demo67 implements Runnable  
{  
    String myString = "Yes ";
    
    public void run()  
    {  
        this.myString = "No ";  
    }
    
    public static void main(String[] args)  
    {  
        Demo67 t = new Demo67();  
        Thread thread=new Thread(t); 
        System.out.println(thread.getId());
        System.out.println(Thread.currentThread().getId());
        for (int i=0; i < 10; i++)  
            System.out.print(t.myString);  
    }  
} 


인쇄 결과는 확실하지 않은데, 왜요?두 개의 라인이 있는데 하나는 t이고 하나는main에서 시작하는 메인 라인이기 때문이다.
The 'main()' method in Java is referred to the thread that is running, whenever a Java program runs. It calls the main thread because it is the first thread that starts running when a program begins. Other threads can be spawned from this main thread. The main thread must be the last thread in the program to end. When the main thread stops, the program stops running.
Main thread is created automatically, but it can be controlled by the program by using a Thread object. The Thread object will hold the reference of the main thread with the help of currentThread() method of the Thread class.
번역
main () 방법은 실행 중인 루틴을 대표합니다. 모든 자바 프로그램의 실행이 먼저main 루틴을 생성하고, 다른 루틴은 파생되며, 프로그램에서 실행되는 마지막 루틴이기 때문에main 루틴이 멈추면 프로그램이 실행되지 않습니다.
main 루틴은 자동으로 생성됩니다. main 루틴의 '인용' 을 받은 Thread 대상에 의해 제어될 수 있습니다.이 방법은 Thread입니다.currentThread.

class MainThread 
{
    public static void main(String args [] ) 
    {
        Thread t = Thread.currentThread ( );

        System.out.println ("Current Thread : " + t);
        System.out.println ("Name : " + t.getName ( ) );
        System.out.println (" ");
        
        t.setName ("New Thread");
        System.out.println ("After changing name");
        System.out.println ("Current Thread : " + t);
        System.out.println ("Name : " + t.getName ( ) );
        System.out.println (" ");

        System.out.println ("This thread prints first 10 numbers");
        
        try 
        {
            for (int i=1; i<=10;i++) 
            {
                System.out.print(i);
                System.out.print(" ");
                Thread.sleep(1000);
            }
        } 
        catch (InterruptedException e) 
        {
            System.out.println(e); 
        }
   }
}



좋은 웹페이지 즐겨찾기