「ict_note_java_sample_thread1」の編集履歴(バックアップ)一覧はこちら
追加された行は緑色になります。
削除された行は赤色になります。
Thread の活性化、非活性化の切り替えを行うクラスのサンプル
public class AppMain
{
public static void main(String[] args)
{
ThreadTest th = new ThreadTest();
th.start();
th.issleep = true;
try {
if (th.isAlive()) {
// threadが死んでる場合はもう一度生成すべき!!!
th.mark = 5;
th.issleep = false;
synchronized (th) {
th.notify(); //復活
}
// スレッド休み
th.issleep = true;
// die Thread
th.isend = true;
}
}
catch (Exception e) {
e.printStackTrace();
}
}
}
public class ThreadTest extends Thread {
public int mark = 0;
public boolean issleep = false;
public boolean isend = false;
public void run() {
while (!isend) {
if (issleep) {
try {
synchronized (this) {
wait();
}
}
catch (Exception e) {
e.printStackTrace();
continue;
}
}
System.out.println("起動中 mark=" + mark);
}
}
}
Thread の活性化、非活性化の切り替えを行うクラスのサンプル
public class AppMain
{
public static void main(String[] args)
{
ThreadTest th = new ThreadTest();
th.start();
th.issleep = true;
try {
if (th.isAlive()) {
// threadが死んでる場合はもう一度生成すべき!!!
th.mark = 5;
th.issleep = false;
synchronized (th) {
th.notify(); //復活
}
// スレッド休み
th.issleep = true;
// die Thread
th.isend = true;
}
}
catch (Exception e) {
e.printStackTrace();
}
}
}
public class ThreadTest extends Thread {
public int mark = 0;
public boolean issleep = false;
public boolean isend = false;
public void run() {
while (!isend) {
if (issleep) {
try {
synchronized (this) {
wait();
}
}
catch (Exception e) {
e.printStackTrace();
continue;
}
}
System.out.println("起動中 mark=" + mark);
}
}
}