Main 에서 네트워크 작업을 수행하면 안됨 !! AsyncTask 를 사용하여 작업을 수행해야 됨


This exception is thrown when an application attempts to perform a networking operation on its main thread. Run your code in AsyncTask:


출처 :

http://stackoverflow.com/questions/6343166/how-to-fix-android-os-networkonmainthreadexception




소켓의 accept 을 응용한 형태로 구현해 봤습니다.

이 형태가 맞는지 ? 원본 소스를 보지 않았는데 나중에 시간되면 한번 봐봐야지요 -_-;


아래 부분을 좀 수정 해야 될거 같은데 흠..

Thread.sleep(Long.MAX_VALUE);


결과 : 


Thread가 2개가 유지 됨 / 1,2 => 2,3 => 3,4 이런 형태로 유지


start

create : 274064735

create : 841752171

close : 274064735

create : 592705150

close : 841752171

create : 244621161

close : 592705150

package kr.co.jwsnt.libs.test;


public class TeNotifyRecur{
	
	/*
	 * 결과
	 * 
	 */

	public static void main(String[] args){
		new TeNotifyRecur();
	}
	
	class TeThread extends Thread{
		
		private TeNotifyRecur p;
		
		public TeThread(TeNotifyRecur p){
			this.p = p;
		}
		
		@Override
		public void run() {
			try {
				Thread.sleep(2000);
				p.reconnect();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
	
	TeNotifyRecur(){

		System.out.println("start");
		
		connect();
		
		while(true){
			try {
				Thread.sleep(Long.MAX_VALUE);
			} catch (InterruptedException e) {
				break;
			}
		}
		System.out.println("end");
		
	}
	
	private void connect()
	{
		Object obj = new Object();
		System.out.println( "create : " + obj.hashCode() );

		try{
			Thread t = new TeThread(this);
			t.setDaemon(true);
			t.start();
			startWait();
		}catch(Exception e){
			System.out.println( "conn : " + e.toString() );
		}finally{
			close(obj);	
		}
	}
	
	private void close(Object obj){
		System.out.println( "close : " + obj.hashCode() );
		obj = null;
	}
	
	public synchronized void reconnect(){
		notify();
		connect();
	}
	
	public synchronized void startWait() throws InterruptedException{
		wait();
	}
}


스레드를 활용한 wait 처리를 구현하기 위해 테스트한 결과를 기록해 두었음.


평소에 thread를 잘 활용하지 않으니 이런 문제가 발생한거 같은데 좀더 많은 탐구가 필요한 것으로 사료 됨.

응용하면 consumer, producer형태에 사용할 수 있음. 이번에 mq관련 프로젝트에서 아래 내용을 활용해서 적용.


socket listen => client socket 개별 생성 => map에 메시지 넣고 wait

mq consumer thread로 동작 메시지 수신 시 map에서 꺼내 notify => client socket은 응답 메시지 작업 후 close


그냥 주저리 내용이므로 참조만 바랍니다. 볼 사람이나 있을려나 훗~


결과 :


job-1

job-2

waitStart-1

run-1

run-2

waitEnd-1

waitEnd-2

run-3

waitStart-2

job-3


public class TeParent {

	class TeChild implements Runnable{
		TeParent p;
		TeChild(TeParent p){
			this.p = p;
		}
		
		@Override
		public void run() {
			try {
				System.out.println("run-1");
				Thread.sleep(3000);
				System.out.println("run-2");
				this.p.waitEnd();
				System.out.println("run-3");
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}
	
	public synchronized void waitStart() throws InterruptedException{
		System.out.println("waitStart-1");
		wait();
		System.out.println("waitStart-2");
	}
	
	public synchronized void waitEnd(){
		System.out.println("waitEnd-1");
		notify();
		System.out.println("waitEnd-2");
	}
	
	TeParent(){
		try{
			System.out.println("job-1");
			new Thread(new TeChild(this)).start();;
			System.out.println("job-2");
			waitStart();
			System.out.println("job-3");
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
	public static void main(String[] args){
		new TeParent();
	}
}


+ Recent posts