Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Thread problem  RSS feed

 
saikrishna cinux
Ranch Hand
Posts: 689
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
import java.io.*;
class MyRun1 implements Runnable{
int count=0;
boolean thread2=false;
long position=0;

public synchronized void fileRead()
{

try
{

RandomAccessFile in=new RandomAccessFile(new File("1.txt"),"r");

// FileInputStream fstream = new FileInputStream(new File("1.txt"));

//DataInputStream in = new DataInputStream(fstream);

//while (in.readLine()!=null)
while (true)
{
if(thread2==true)
{
System.out.println ("in thread2^^^^^^^^^");
in.seek(position);
System.out.println ("t2"+in.readLine() +" " +count +" " +Thread.currentThread());
}

if(count>=23){
System.out.println ("in 23$$$$$$$$$$");
position=in.getFilePointer();
//notifyAll();
thread2=true;
break;
}
System.out.println (in.readLine() +" " +count +" " +Thread.currentThread());
count++;
}

in.close();
}
catch (Exception e)
{
System.err.println("File input error");
}



System.out.println(count);


}

public void run(){fileRead();}

}
public class Test{

public static void main(String args[])
{
MyRun1 r1=new MyRun1();



Thread t1=new Thread(r1);
Thread t2=new Thread(r1);
t1.start();
t2.start();

//System.out.println("asdasfsdfdsfsdfdsf");
}

}

hi all,
Here is one program i am developing to implement thead funcionality to read a file using RandomAccessFile so that the first thread must read first 50 lines and the second thread must read the next 50 line of the file

the above program is not working well please check it out if you can make it work properly

thanks in advance

regards
saikrishna
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16026
87
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"the above program is not working well please check it out if you can make it work properly"

Please tell the details. What does "is not working well" mean? What do you expect the program to do, what does it really do and how is that different from what you expect?

Also, please use code tags when you post source code, so that the forum software can format your code in a readable way.
[ March 14, 2007: Message edited by: Jesper Young ]
 
saikrishna cinux
Ranch Hand
Posts: 689
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry for not keeping my code in code block.

Here is the clear explanation of my program


1.I want to read a File using RandomAccessFile using 2 threads so that the First Thread must read the first 50 lines from the file and the Second thread must read the next 50 lines of the File.

so the above program which i am trying is unable to run properly...

My idea is certainley good but unable to get the solution.

can anybody help me out to solve this problem programatically


thanks in advance


regards

saikrishna
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16026
87
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so the above program which i am trying is unable to run properly...

Explain what that means. What does the program do when you run it? Why is that not what you expect?
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


You will have no control over which Thread runs or how long it runs before the other Thread gets a chance. This sounds like a homework assignment - go read your chapter on Threads again.

Bill
 
saikrishna cinux
Ranch Hand
Posts: 689
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by William Brogden:


You will have no control over which Thread runs or how long it runs before the other Thread gets a chance. This sounds like a homework assignment - go read your chapter on Threads again.

Bill

No william,
I read the threads concepts very well,

but this programming is not working properly,i also used synchronized method for stopping the the second thread the first thread is able to read the first 23 lines but the second thread is just reading the 24ht line and it stops.

For your kind information this is not my homeowrk and i am not a school boy ,i am now working in top software companies,this is my idea ,my thought to do this program.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!