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");
	}
}

댓글

이 블로그의 인기 게시물

Java InetAddress.getHostName()/getHostAddress

Scanner 클래스로 -1이 입력될 때까지 양의 정수를 입력 받아 저장(List객체)하고 검색하여 가장 큰 수를 출력하는 프로그램을 작성하라.

Text editing shortcuts that work on eclipse