synchronized同期静的方法と方法
1245 ワード
synchronized同期静的メソッドとメソッドは、クラスオブジェクトをロックフラグとして使用します.
public class TraditionalThread { public static void main(String[] args) { new TraditionalThread().init(); } private void init(){ final Outputer outputer = new Outputer(); new Thread(new Runnable() { @Override public void run() { while (true){ try { Thread.currentThread().sleep(10); } catch (InterruptedException e) { e.printStackTrace(); } outputer.output(“redbull”); }
}
public class TraditionalThread { public static void main(String[] args) { new TraditionalThread().init(); } private void init(){ final Outputer outputer = new Outputer(); new Thread(new Runnable() { @Override public void run() { while (true){ try { Thread.currentThread().sleep(10); } catch (InterruptedException e) { e.printStackTrace(); } outputer.output(“redbull”); }
}
}).start();
new Thread(new Runnable() {
@Override
public void run() {
while (true){
try {
Thread.currentThread().sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
outputer.output2("apple");
}
}
}).start();
}
static class Outputer{
void output(String message){
//
synchronized (Outputer.class){
for(int i = 0;i
}