자바에서 쓰레드가 주기적으로 특정 작업을 수행하도록 하고 싶을 땐 어떻게 하면 되나요?

vudghkzm의 이미지

자바로 멀티 쓰레드 소켓 클라이언트 프로그램을 하나 만들고 있습니다.
서버에 정해진 숫자만큼의 쓰레드를 생성하고, 각 쓰레드는 하나의 소켓을 생성하고 서버에 연결합니다.

그런데 서버측에서는 연결된 소켓이 idle 상태로 정해진 시간만큼 있으면(즉, 어떤 메세지도 주고 받지 않고 있으면) 자동으로 소켓 연결을 끊습니다. 하지만 이 주기보다 짧은 시간내에 특정 메세지를 서버쪽으로 보내게 되면 서버는 소켓이 non-idle 상태라고 인식하고, 소켓 연결을 끊지 않는거 같습니다.

그래서 생각해 낸게 다음과 같습니다.

1. 주기적으로 수행될 수 있는 메쏘드를 만든다.
2. 메쏘드가 실행되면 소켓이 연결되어 있는지 그렇지 않은지 검사한다.
3. 소켓이 끊어져 잇으면 소켓을 다시 연결시킨다. 만약 소켓이 연결되어 있다면 소켓 연결 유지 메세지를 서버 쪽으로 보낸다.

위와 같은 방법인데요.
위에서 "주기적으로 수행될 수 있는 메쏘드"를 만드는 법을 모르겠습니다.

어떻게 만드나요?

kfmes의 이미지

method 안에 while()무한루프를 만들어두고
물론 종료조건도 넣어줘야되겠죠?

쓰레드니깐, sleep() 을 써서 만들면 될거같군요

while(true){
	if(flag) // 종료조건
		break;
	// 할 일
	sleep(1000);// 1000 msec = 1sec
	
}

----------------------------------------

M.W.Park의 이미지

대충 제가 쓰던 코드를 테스트도 없이 올려봅니다. 8)

public class Foo {
    public static final int DELAY = 5 * 1000;
    Timer timer = null;

    public void start() {
	if (timer == null) {
	    timer = new Timer();
	    timer.schedule(new TimerTask() {
		    public void run() {
			doIt();
		    }
		}, DELAY);
	}
    }

    public void stop() {
	if (timer != null) {
	    timer.cancel();
	    timer = null;
	}
    }

    public void doIt() {
	// do something here...
	try {
	    if (timer != null) {
		timer.schedule(new TimerTask() {
			public void run() {
			    doIt();
			}
		    }, DELAY);
	    }
	} catch(IllegalStateException e) {};
    }
}

-----
오늘 의 취미는 끝없는, 끝없는 인내다. 1973 法頂

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.