수정>자바 멀티 쓰레드 프로그래밍., 도와주세요 ㅜㅠㅜ

lgw2928의 이미지

안녕하세요 ~
컴공과 다니고 있는 비루한 학부생입니다 ㅠㅠ
저가 이번에 자바를 공부한다고 나름열심히 하고 있는데
쓰레드 프로그래밍을 하다 막혀서 쩔쩔매고 있네요.,

문제는 쓰레드 개수를 입력 받고 쓰레드를만들고
해당쓰레에 n번쓰레드라는 이름을 만들고 해당쓰레드를 사용하여
1번쓰레드 입력받고 2번쓰레드 입력받고 3번쓰레드 입력받고 이렇게 차례대로
같은 텍스트파일에 텍스트를 쓰는것인데요

테스트는 쓰레드3개를 사용하기위해서 모듈연산자를 3으로 했구요.,
i값을 스테틱변수로 해서 쓰레드 id와 비교해서 같지않으면 쓰레드가 wait()하고
같으면 쓰레드 실행되고 또 모듈연산해주고 notifyAll() 해서 쓰레드를 전부 깨우고
다시 i값과 id를 비교해서 wait()하는 반복적인 형태입니다. .

코드상 맞는거 같은데
synchronized()파라미터를 잘못했는지 1번쓰레드입력받고 넘어 가지를 않네요 ㅠㅠ
설명이 빈약하지만 코드를 보시고 잘못된 점 지적해주시면 감사하겠습니다 ㅠㅠ
코드를 보시면

public class Main {
	public static int ThreadNum ;
	public static ThreadRun mThread[];
	public static int j ;
	public static int i = 0;
 
	public static void main(String args[]) throws Exception
	{
 
 
			System.out.println("스레드 생성 개수:");
			Scanner num = new Scanner(System.in);
			ThreadNum = num.nextInt();
 
			//fw = new FileWriter("test2.txt");
 
			mThread = new ThreadRun[100];
			for(j=0 ; j<ThreadNum ; j++ )//입력받은 개수만큼 쓰레드 생성
			{	
 
				mThread[j] =  new ThreadRun(j);
				mThread[j].start(); 
 
			}
	}
}
public class ThreadRun extends Thread {
 
	private String source ;
	private String source2 ;
	private int id;
 
 
 
	public ThreadRun(int id)
	{	
		this.id = id;
 
	}
 
	public void run()
	{	
 
			while(true)
			{
				synchronized(this)
				{	
 
 
					while(id!=Main.i)
					{	
 
 
						try 
						{
							wait();
 
						} 
						catch (InterruptedException e) 
						{
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
 
					}
				              try
					{	
 
 
						System.out.println((id+1)+"번째 쓰레드:");
						Scanner st = new Scanner(System.in);
						source2 = st.next();
						source = String.format("%d번 쓰레드:%s\r\n", (id+1),source2);
						char input[] = new char [source.length()];
						source.getChars(0,source.length(),input,0);
						Main.fw.write(input);
						st.close();
						Main.fw.close();
					}
					catch(Exception e) 
						{
						System.out.println("can not Write");
						}
                                       	Main.i = (Main.i+1)%3;
					notifyAll(); 
 
		         }
 
		} 
	}
 
}

댓글 달기

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
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.