File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Thread Flow

 
vineet kumar chaudhry
Ranch Hand
Posts: 45
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Java Loverz

[code]

package clientserver;

import java.net.*;
import java.util.Scanner;
import java.io.*;

public class Server{

byte[] data;
Socket socket;
private int ID;
ServerSocket serverSocket;
int in;
int count = 0;
third ob = new third();
public Server() {
try {

serverSocket = new ServerSocket(7777);

System.out.println("i am server & listening...");
while (true) {


socket = serverSocket.accept();

ThreadExample th = new ThreadExample(new NewThread(ob , socket ));
th.start();
System.out.println("////////////Thread State ----> " + th.getState()+"////////////////");
}


} catch (Exception e) {
e.printStackTrace();
}
}

public static void main(String[] args) {
new Server();
}
}

class ThreadExample extends Thread
{

public ThreadExample(Runnable r){
super(r);
}


}
class NewThread implements Runnable
{
private third ob1;
private Socket socket;

NewThread(third ob,Socket socket)
{
this.socket = socket;
this.ob1 = ob;
}

public void run()
{
System.out.println("Thraead name --->" + Thread.currentThread().getName());


ob1.receiveFile( socket );


}

}

class third
{
synchronized public void receiveFile(Socket socket){


BufferedInputStream bis;
BufferedOutputStream bos;
String str ="";

try{

System.out.println("a client connect");
System.out.println("------1--------" +socket.isClosed());

bis = new BufferedInputStream(socket.getInputStream());

while (bis.available()>0){
char ch = (char)bis.read();
//System.out.println(ch);
str = str + ch;

}
//System.out.println("Data from client --->" + str);
System.out.println("???Thraed State --->" + Thread.currentThread().getState());
System.out.println("???Thread Name --->" + Thread.currentThread().getName());



File xmlFile = new File("C:\\Data.xml");
if (!xmlFile.exists()) {

//String outputdirectoryabsolutepath = outputDirectory
//.getAbsolutePath();

//File historyXmlFile = new File(outputdirectoryabsolutepath);

xmlFile.createNewFile();
// System.out.println("Created File");
FileOutputStream fileoutputstream = new FileOutputStream(
xmlFile);
// System.out.println("Opened STream");
String temp = "<history>\n</history>";
System.out.println(temp);

byte tempBytes[] = temp.getBytes();
// System.out.println("Got byte Data");

fileoutputstream.write(tempBytes);
// System.out.println("Written the data");
fileoutputstream.close();

}


FileInputStream fin = new FileInputStream(xmlFile);

int length = fin.available();
System.out.println(length);
System.out.println(length);

fin.close();

Scanner scanner = new Scanner(xmlFile).useDelimiter("</history>");
//System.out.println("Got scanner");
String line = scanner.next();
//System.out.println("Line --->" + line);
// System.out.println("Got the line object");
//System.out.println("DATA --->"+ str);
//System.out.println("line.substring(0,(length-11)) --->" + line.substring(0,(length-11)));
String newLine = line.substring(0, (length - 11))
+ str;
// System.out.println(newLine);

byte b[] = newLine.getBytes();

FileOutputStream fileoutputstream = new FileOutputStream(xmlFile);

fileoutputstream.write(b);
fileoutputstream.close();
System.out.println("Completed");

//bos.write(by);
//bos.close();
}

catch (Exception e){
e.printStackTrace();
}

}
}
[code]


I wanna know

When first thread holds the lock on synchronized method ,wht does the other thread do

1>Does it enters the run mehod or not

I need to verify that the second thread is waiting for its chance while another is working
 
Madhan Sundararajan Devaki
Ranch Hand
Posts: 312
Java MS IE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I believe, when multiple threads are involve, all of them will enter the run method and proceed upto the call to the synchronized receiveFile method. However, the first thread to enter the receiveFile method will block the rest of the threads until completion.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic