私は全く新しい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());
}
}
}
あなたの質問は具体的ですか? [質問] – pvg
を見てください。あなたのコードにかなりの問題があります。私はあなたがマルチスレッドを試みる前に、Javaについてもっと学ぶことをお勧めします。 –