スレッドの概念を完全に理解していませんでした。スレッドが終了したときに実行コードを確認します。
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ミリ秒ごとに1回ずつ繰り返すスレッドを作るにはどうすればよいですか?私は考えていた
while(true) { t5.start; }
私は確かに分かりません。
whileループでtry {...} catch(InterruptedException e)(...}を追加することをお勧めしますが、これはそのjistです。 –
ありがとう、私は試してみます:) – Master345
作品完全に、私はまた、スレッドを繰り返す回数を追加しました – Master345