como crear un subproceso de este hilo en java
Publicado por felix (3 intervenciones) el 16/11/2011 00:36:41
como crear un subproceso de este hilo en este programa cliente servidor de java socket
{
public static class ThreadServicio extends Thread {
public ThreadServicio(String str) {
super(str);
}
public void run () {
for (int i = 0; i < 10 ; i++)
System.out.println(i + " " + getName());
System.out.println("Termina thread " + getName());
}
public static void main (String [] args) {
new ThreadServicio("Socket").start();
new ThreadServicio("s1").start();
System.out.println("Termina thread main");
}
public void procesar(Socket Canal) throws IOException{
int Num1,Num2,res = 0;
String OP;
DataOutputStream canal_salida_Serv = new DataOutputStream (Canal.getOutputStream());
DataInputStream canal_entrada_Serv = new DataInputStream (Canal.getInputStream());
OP = canal_entrada_Serv.readUTF();
System.out.println( OP );
Num1 = canal_entrada_Serv.readInt();
System.out.println( Num1 );
Num2 = canal_entrada_Serv.readInt();
System.out.println( Num2 );
new ThreadServicio("Primero").start();
new ThreadServicio("Segundo").start();
if (OP.equals("+")){
res= Num1 + Num2;
}
if (OP.equals("-")){
res= Num1 - Num2;
}
if (OP.equals("*")){
res= Num1 * Num2;
}
if (OP .equals("/")){
res= Num1 / Num2;
}
System.out.println( res );
canal_salida_Serv.writeInt(res);
Canal.close();
}
public static class servidor
{
public static void main( String args[] ) throws IOException
{
Servicio miServicio = new Servicio();
boolean hayErrores=true;
ServerSocket s = (ServerSocket)null;
Socket s1 = (Socket) null;
int puerto = 30087;
while (hayErrores)
{
hayErrores=false;
try
{
s=new ServerSocket(puerto);
} catch( IOException e )
{
hayErrores = true;
System.out.println( "Error al conectar al puerto: "+e );
}
}
while( true )
{
try
{
s1 = s.accept();
} catch( IOException e )
{
System.out.println( "Error al aceptar conexión: "+e );
}
miServicio.procesar(s1);
} // Fin del ciclo infinito del servidor
} // Fin del método main
}
}
public void procesar(Socket s1) {
// TODO Auto-generated method stub
}
}
{
public static class ThreadServicio extends Thread {
public ThreadServicio(String str) {
super(str);
}
public void run () {
for (int i = 0; i < 10 ; i++)
System.out.println(i + " " + getName());
System.out.println("Termina thread " + getName());
}
public static void main (String [] args) {
new ThreadServicio("Socket").start();
new ThreadServicio("s1").start();
System.out.println("Termina thread main");
}
public void procesar(Socket Canal) throws IOException{
int Num1,Num2,res = 0;
String OP;
DataOutputStream canal_salida_Serv = new DataOutputStream (Canal.getOutputStream());
DataInputStream canal_entrada_Serv = new DataInputStream (Canal.getInputStream());
OP = canal_entrada_Serv.readUTF();
System.out.println( OP );
Num1 = canal_entrada_Serv.readInt();
System.out.println( Num1 );
Num2 = canal_entrada_Serv.readInt();
System.out.println( Num2 );
new ThreadServicio("Primero").start();
new ThreadServicio("Segundo").start();
if (OP.equals("+")){
res= Num1 + Num2;
}
if (OP.equals("-")){
res= Num1 - Num2;
}
if (OP.equals("*")){
res= Num1 * Num2;
}
if (OP .equals("/")){
res= Num1 / Num2;
}
System.out.println( res );
canal_salida_Serv.writeInt(res);
Canal.close();
}
public static class servidor
{
public static void main( String args[] ) throws IOException
{
Servicio miServicio = new Servicio();
boolean hayErrores=true;
ServerSocket s = (ServerSocket)null;
Socket s1 = (Socket) null;
int puerto = 30087;
while (hayErrores)
{
hayErrores=false;
try
{
s=new ServerSocket(puerto);
} catch( IOException e )
{
hayErrores = true;
System.out.println( "Error al conectar al puerto: "+e );
}
}
while( true )
{
try
{
s1 = s.accept();
} catch( IOException e )
{
System.out.println( "Error al aceptar conexión: "+e );
}
miServicio.procesar(s1);
} // Fin del ciclo infinito del servidor
} // Fin del método main
}
}
public void procesar(Socket s1) {
// TODO Auto-generated method stub
}
}
Valora esta pregunta


0