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);
}
}
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Exception in thread main java.lang. NoClassDefFoundError 오류 해결 방법즉,/home/hadoop/jarfile) 시스템은 Hello World 패키지 아래의class라는 클래스 파일을 실행하고 있다고 오인하여 시스템의 CLASSPATH 아래 (일반적으로 현재 디렉터리를 포함) Hell...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.