0
各スレッドが排他モードでインクリメントするmyCount変数をシリアル化するにはどうすればよいですか?後で次のスレッドは、myCountの新しい値に基づいてそれを増やしますか? myCountはスレッドごとに順番に増加しますか?次のようにmyCount変数をシリアル化する方法は?
現在、私の結果:
starting main thread----
-----this is Main1
ex Thread:2
ex Thread:3
-----this is Main3
ex Thread:4
-----this is Main4
ex Thread:5
-----this is Main5
-----this is Main6
ex Thread:7
-----this is Main8
ex Thread:9
-----this is Main10
ex Thread:11
---------------コード---------------- ---
class MySamThread extends Thread {
public static int myCount=0;
public void run() {
while(MySamThread.myCount<=10){
try{System.out.println("ex Thread:"+(++MySamThread.myCount));
Thread.sleep(100);
}catch (InterruptedException iex){
System.out. println("exception in thread:"+iex.getMessage());}
}}}
public class RunSamThread {
public static void main (String a[]){
System.out.println("starting main thread----");
MySamThread mst=new MySamThread();
mst.start();
while(MySamThread.myCount<=10){
try{
System.out.println("-----this is Main"+
(++MySamThread.myCount));
Thread.sleep(100);
} catch (InterruptedException iex){``
System.out.println("exception in main
thread"+iex.getMessage());}
}}}
ありがとうございました!
@ Luluのserializeの使用が適切ですが、「myCountへのアクセスをシリアライズする」と言えるかもしれません。 – markw
非常に遅れて申し訳ありません!私は今日のフォーラムに戻っています。ここであなたの助けに基づいて試してみます。あなたの親切な返信をありがとう! – Lulu