• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Thread Flow

 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 312
MS IE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic