스레드의 개념을 완전히 이해하지 못했습니다. 몇 가지 질문이 있습니다.스레드가 완료되면 코드 실행을 확인하십시오.
ExecCommand.java
// I don't know how this work, for now
package therads;
// Here we will have the methods and run them from the Main.java
public class ExecCommand implements Runnable
{
String name;
int time;
public ExecCommand(String s,int amount)
{
name = s;
time = amount;
}
// Run method (Runnable)
public void run()
{
try
{
// What to execute when the thread is started
System.out.printf("%s is sleeping for %d\n",name,time);
Thread.sleep(time);
System.out.printf("%s is done\n",name);
}
catch(Exception e)
{
}
}
// This dosen't work when the thread is stopped
public void stop()
{
try
{
System.out.printf("STOPPED!");
}
catch(Exception e)
{
}
}
// This dosen't work when the thread is started
public void start()
{
try
{
System.out.printf("Started!");
}
catch(Exception e)
{
}
}
}
와 내가 그를 전화 :
Main.java
Thread t5 = new Thread(new ExecCommand("Good Function",1000));
t5.start();
내가 원하는 우리는 다음과 같은 코드가 있다고 가정 ~
println()
스레드가 시작되면 "시작됨", 완료되면 "중지됨". 것이 가능하다?스레드가 완료되면 메모리에서 완전히 종료됩니다. 그렇지 않다면 어떻게 할 수 있습니까?
키를 누를 때까지 1000 밀리 초마다 한 번씩 반복되는 스레드를 만들 수 있습니까? 나는에 대해 생각하고 있었다
while(true) { t5.start; }
나는 잘 모른다.
while 루프에서 try {...} catch (InterruptedException e) (...} catch를 추가하는 것이 좋겠지 만 이는이 jist입니다. –
감사합니다. 시도해 보겠습니다 :) – Master345
작동합니다 완벽하게, 나는 또한 스레드를 반복 할 얼마나 많은 시간을 추가했습니다. – Master345