2017-10-08 6 views
0

私は全く新しいJavaですが(C++に関する知識はありますが)、既存のトピックについては答えが見つかりません。静的メソッドでのJavaマルチスレッドの作成

私は静的メソッドを含むパブリッククラスを持っています。これは、このクラスのインスタンスをインスタンス化する多数のスレッドを作成し、各インスタンスはブロック処理を行います。

あなたがいること(インスタンス変数にアクセスしようとしている...

public class MyClass extends java.awt.Frame { 
    String myString1; 
    String myString2; 
    String myString3; 
    private ActionListener aL; 
    private volatile boolean boolRunning; 

    public Runnable r = new Runnable(){ 
     @Override 
     public void run() { 
      MyClass q = new MyClass(MyClass.this); 
      synchronized (this) { 
       MyClass.this.notify(); 
      } 
     } 
    } 

    private MyClass(String toParse){ 
     String[] parsed = toParse.split(":"); 
     this.myString1 = parsed[0]; 
     this.myString2 = parsed[1]; 
     this.myString3 = parsed[2]; 
     instantiateFrame(); 
    } 

    public void instantiateFrame(){ 
     this.setBounds(...); 
     this.setLayout(...); 
     Button btn = new Button("Submit"); 
     [...] 
     this.aListener = new ActionListener(){ 
      @Override 
      public void actionPerformed(ActionEvent e){ 
       /************************/ 
       /* Treatment hear  */ 
       /* Increment static Var */ 
       /************************/ 
       synchronized (MyClass.this){ 
        boolRunning = true; 
        MyClass.this.notify(); 
       } 
       MyClass.this.dispose(); 
      } 
     }; 
     btn.addActionListener(aL); 
     this.add(btn); 
     this.setVisible(true); 
     /* Wait for btn clicked */ 
     /* Blocking method */ 
     synchronized(this) { 
      try { 
       this.wait(); 
       boolRunning = true; 
      } catch (InterruptedException ex) { 
       Logger.getLogger(MyClass.class.getName()).log(Level.SEVERE, null, ex); 
      } 
     } 
    } 

    public static int startThreads(String[] toParseTable){ 
     Thread[] t = new Thread[toParseTable.length]; 
     for (int i = 0; i < toParseTable.length; i++) { 
      t[i] = new Thread(MyClass.r);  // Not compiling : Non static variable "r" cannot be referenced from a static context 
      t[i].start(); 
     } 
     /* Wait for all Threads */ 
     for (Thread th : t) { 
      try { 
       th.join(); 
      } catch (InterruptedException ex) { 
       Logger.getLogger(MyClass.class.getName()).log(Level.SEVERE, null, ex); 
      } 
     } 
     return MyClass.someStaticVar; 
    } 

    public static void main(String[] args) { 
     String[] someStrings = {"...:...:...", 
           "...:...:...", 
           "...:...:..."}; 
     try { 
      System.out.println(MyClass.startThreads(someStrings)); 
     } catch (CustomThrownException e) { 
      System.err.println(e.getMessage()); 
     } 
    } 
} 
+4

あなたの質問は具体的ですか? [質問] – pvg

+1

を見てください。あなたのコードにかなりの問題があります。私はあなたがマルチスレッドを試みる前に、Javaについてもっと学ぶことをお勧めします。 –

答えて

0

を、私はこのクラスのフィールドとしてのRunnableを追加しようとしたが、私はそれを正しく行う方法について少し混乱していますインスタンスオブジェクトに属していないがクラスに属していないメソッドでは、オブジェクトに属する変数です)。 static関数と変数はグローバルクラスですが、暗黙的にthisが存在しないため、インスタンスから変数にアクセスすることはできません。

class Foo { 
    private int value = 2; 
    private static int woz = 6; 

    public void foo() { 
    System.out.println(value); // ok, access instance variable in instance method with implicit this object 
    System.out.println(woz); // ok, access 'class global' variable 
    } 


    public static void bar() { // is a 'class' method, not an instance method 
    System.out.println(value); // can't because there is no 'this' object here 
    System.out.println(woz); // ok, access 'class global' variable 
} 
+0

私は静的メソッドについて知っていますが、私はそれを他の方法で行う方法はわかりません! –

+0

私の経験では、シングルトンや定数で使われるとき以外は静的変数を使う必要はありません。 –

関連する問題