You are not logged in.

  • Login

1

Sunday, December 21st 2008, 8:29pm

Thread I/O test - gehts vlt besser?

Nabend Leute,
Ich bin neu hier im Board und dachte mir ich veröffentliche als Einstand mal eben einen kleinen Code...
Ich bin relativ neu im umgang mit Java und habe dieses Prog. als vorarbeit auf einen Server welcher zwischen mehreren
Clients(Threads) I/O durchführen kann (soll mal ein chatserver werden).
Ich würde gerne wissen ob dies schon eine relativ gute Lösung ist oder ob ich mich mit meinem halbwissen doch schon zu sehr
verrannt habe.
Wer Lust und Zeit hat kanns sich ja mal angucken.

Über feedback freue ich mich natürlich immer :)

Java Quellcode

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
// Thread IO Example -- by Bakero
// + mehrere Threads, welche ein und die Selbe aufgabe erledigen (zB verb. clients
//	 abfertigen) starten und deren IO regeln.
 
 
package thread_io;
 
import java.io.*;
public class Thread_io {
 
	public static void main(String[] args)
	{
//code für die comm von 2 threads bzw father2child
		try
		{
			PipedInputStream thread1_out = new PipedInputStream();
			PipedOutputStream thread1_outt = new PipedOutputStream(thread1_out);
 
			PipedOutputStream thread1_inn = new PipedOutputStream();
			PipedInputStream thread1_in = new PipedInputStream(thread1_inn);
//---------------------------------------------------------------------------
 
//möglichkeit für egal anzahl an threats
			int thread_id = 0; 	//id des ersten threads + current thread
			int max_threads = 3;   //anzahl der maximal möglichen threads
			int counter = 0;   	//allgemeiner counter
			PipedInputStream[] thread_out = new PipedInputStream[max_threads];
			PipedOutputStream[] thread_outt = new PipedOutputStream[max_threads];
			PipedOutputStream[] thread_inn = new PipedOutputStream[max_threads];
			PipedInputStream[] thread_in = new PipedInputStream[max_threads];
			//Pipes Conf
			while(counter <= (thread_inn.length-1))
			{
				thread_out[counter] = new PipedInputStream();
				thread_outt[counter] = new PipedOutputStream(thread_out[counter]);
				thread_inn[counter] = new PipedOutputStream();
				thread_in[counter] = new PipedInputStream(thread_inn[counter]);
				counter++;
			}
			counter=0;          	//counter reset
//--------------------------------------------------------------------------
 
//Einzelnen thread starten
			Thread_1 t1 = new Thread_1(thread1_in,thread1_outt);
//--------------------------------------------------------------------------
 
// beliebige anzahl eines threads starten welche die gleiche aufgabe lösen
			Thread_[] t = new Thread_[max_threads]; //threads initialisieren
 
			while(thread_id <= max_threads-1)   //Threads starten
			{
				t[thread_id] = new Thread_(thread_in[thread_id],thread_outt[thread_id],thread_id);
				System.out.println("Starte Thread : " + thread_id);
				t[thread_id].start();
				thread_id++;
			}
//---------------------------------------------------------------------------
 
//Von allen threads lesen und anschliesend senden			
			while(true)
			{
				while(counter <= (max_threads-1))
				{
					System.out.println("Master:lese" + thread_out[counter].read());
					System.out.println("Master:schreibe");
					thread_inn[counter].write(0);
					//System.out.println("Master:lese von 2" + thread_out[counter].read());
					//System.out.println("Master:schreibe an 2");
					//thread_inn[counter].write(3);
					counter++;
				}
				counter=0;
			}
		}
		catch(IOException e)
		{
			System.out.println("I/O ERROR!");
		}
//---------------------------------------------------------------------------
	}
}
class Thread_1 extends Thread
{
	PipedInputStream thread_in = null;
	PipedOutputStream thread_out = null;
	Thread_1(PipedInputStream thread_in,PipedOutputStream thread_out)
	{
		this.thread_in=thread_in;
		this.thread_out=thread_out;
	}
	public void run()
	{
		while(true)
		{
			try{
			System.out.println("Thread:schreibe");
			thread_out.write(1);
 
			System.out.println("Thread:lese" + thread_in.read());
			}
			catch(IOException e)
			{
				System.out.println("Fehler");
			}
		}
 
	}
}
class Thread_ extends Thread
{
	int thread_id;
	PipedInputStream thread_in = null;
	PipedOutputStream thread_out = null;
	Thread_(PipedInputStream thread_in,PipedOutputStream thread_out,int thread_id)
	{
		this.thread_id = thread_id;
		this.thread_in=thread_in;
		this.thread_out=thread_out;
	}
	public void run()
	{
		while(true)
		{
			try{
			System.out.println("Thread"+thread_id+":schreibe");
			thread_out.write(1);
			System.out.println("Thread:habe geschrieben");
			System.out.println("Thread:lese" + thread_in.read());
			}
			catch(IOException e)
			{
				System.out.println("Fehler");
			}
			//catch(NullPointerException b)
			//{
			//	System.out.println("I/O ERROR");
			//}
		}
 
	}
}

Tagging

Social bookmarks