Thread.currentThread().getName()
class Blog { public static void main(String args[]) { Method m = new Method(); (new MyThread(m)).start(); (new MyThread(m)).start(); } } class MyThread extends Thread { Method m; MyThread(Method m) { this.m = m; } @Override public void run() { for (int i = 0; i < 10; i++) m.method(); } } class Method { synchronized void method() { System.out.println(Thread.currentThread().getName() + " : " + "something"); } } class Blog { public static void main(String args[]) { Runnable r = new MyRunnable(); new Thread(r).start(); new Thread(r).start(); } } class MyRunnable implements Runnable { Method m = new Method(); @Override public void run() { for (int i = 0; i < 10; i++) m.method(); } } class Method { synchronized void method() { System.out.println(Thread.currentThread().getName() + " : " + "something"); } }
댓글
댓글 쓰기