Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Use threads to receive and read results from ResultSet

 
bahri sirine
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HI,
I use ORACLE,JAVA..
I want to create two threads , the first one is to rereive the results from the resultset and the second is to read these results .I want that these to threads works in parallel.Any idea please?
 
Claude Moore
Ranch Hand
Posts: 829
7
IBM DB2 Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why dou you want to read a result set and process retrieved data in two separated threads? I can't imagine a scenario where increased complexity would give any advantage back. Why don't you simply fetch data creating some ValueObject items, put them in a collection and then eventually process such collection in a multithreaded fashion?
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Claude Moore wrote:Why dou you want to read a result set and process retrieved data in two separated threads? I can't imagine a scenario where increased complexity would give any advantage back.

My thoughts exactly!
 
bahri sirine
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Claude,
Thanks for your suggestion, but my professor need to implement this method because we work with the Big Data and we need to create threads to optimise the response time . Any idea how to implement this?
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
bahri sirine wrote:but my professor need to implement this method because we work with the Big Data and we need to create threads to optimise the response time . Any idea how to implement this?

Let me guess: to process 165 million rows of data?

Don't forget: CodeRanch is NotACodeMill! So we don't do your homework.
 
bahri sirine
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HI ROEL,
Because when we make the setfetchsize(10 000) for example, we have deduced that there are a lot of time to retreive the 10 000 rows so we need to work in parallel with threads.One thread to retreive data and the other one to read this data.
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And what did you come up with so far?
 
Claude Moore
Ranch Hand
Posts: 829
7
IBM DB2 Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
bahri sirine wrote:HI ROEL,
Because when we make the setfetchsize(10 000) for example, we have deduced that there are a lot of time to retreive the 10 000 rows so we need to work in parallel with threads.One thread to retreive data and the other one to read this data.


You'd better have multiple threads each of which fetches data by paging them..
 
bahri sirine
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Roel,
Yes I know but I don't have any idea how to do this.So I wan't just any help to begin
 
Paul Clapham
Sheriff
Posts: 21581
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
bahri sirine wrote:I want that these to threads works in parallel.


I don't understand what it means for two threads to "work in parallel". If it means that they both run at the same time, then that's a vacuous requirement because two threads always run at the same time unless you write code which prevents that from happening. Otherwise, it's like requiring your apples to be fruit.
 
bahri sirine
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi paul ,
Just I want to work in parallel so to use thread . we make the setfetchsize(10 000) for example, we have deduced that there are a lot of time to retreive the 10 000 rows so we need to work in parallel with threads. One thread to retreive data and the other one to read this data.
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
bahri sirine wrote:Yes I know but I don't have any idea how to do this.So I wan't just any help to begin

The Java Tutorial about Concurrency seems a very good starting point if you have absolutely no clue about threading and concurrency.
 
Claude Moore
Ranch Hand
Posts: 829
7
IBM DB2 Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm far to be an expert of Big Data, anyway I presume that handling big data is by far more complex than reading quickly a lot of rows. So, if your problem is basically an homework assignment, you should focus on implementing key concepts of related algorithms instead of being worried about real-world handling of Big Data. Otherwise it would be better to choose a bid data database product, and look at what it offers. I think you are in the first case, though.
 
bahri sirine
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Roel,
I have already read some tutorials for threads..
 
Paul Clapham
Sheriff
Posts: 21581
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
bahri sirine wrote:One thread to retreive data and the other one to read this data.


So just use an ExecutorService then.
 
bahri sirine
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks paul , Can you give me a simple example to understand this.How can I use the executorService in my problem?
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please ShowSomeEffort! Nobody will do your homework and write the code for you.

You claim to have read some tutorials about threads, so you should be able to write some code using some low-level concurrency classes (e.g. Thread, Runnable,...). Maybe you already have read about the more high-level concurrency classes (e.g. Executors, ExecutorService,...) which can be used for your problem as well. But I didn't see any line of code until this point.

You have to write the code yourself and if you are experiencing any problems/issues you can share them here and people will help you to improve your code or fix the problems you are experiencing. But it seems you are just waiting for someone to write the code for you. Unfortunately CodeRanch is NotACodeMill.
 
bahri sirine
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
I tried to fix this problem but always it return just the first record from the database ?any idea please how can I correct this

code:

 
Claude Moore
Ranch Hand
Posts: 829
7
IBM DB2 Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try to remove statement, so threads are endless. What is the result?
 
bahri sirine
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HI Mr.Claude,
I removed finished=true,and I obtained the same result just the first ROW.
I wan't that the thread 1 write and the thread 2 read in parallel
 
Claude Moore
Ranch Hand
Posts: 829
7
IBM DB2 Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ouch... Try to make reader thread endless...
 
bahri sirine
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how can I make the reader endless..I wan't that when finished become true the reader read ...
 
Claude Moore
Ranch Hand
Posts: 829
7
IBM DB2 Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You started the reader thread, then the writer one, and via join instruction you want the writer wait for the reader finishing. But the whole program will finish and exit before that threads have completed their work. What happens if you print out something for debugging ? Just to see how execution of threads alternates.
 
bahri sirine
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
when I printin the thread2:
while(!finished)
{

int l=reader.read();
System.out.println(l);
System.out.println("thread2");

}
and in the thread 1:

while(rs.next()){
int k= rs.getInt("idetudiant");
writer.write(k);
finished=true;
System.out.println("thread1");
}

I obtained :
thread1
thread1
thread1
thread1
thread1
thread1
thread1
1
thread2

=>So it retreive all the result by thread 2 and then it read only 1 result
 
Claude Moore
Ranch Hand
Posts: 829
7
IBM DB2 Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So, what is your guess about?
 
bahri sirine
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't undrstand really what happened ...Any Idea please how can I correct this
 
Claude Moore
Ranch Hand
Posts: 829
7
IBM DB2 Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm afraid you have to review Oracle's trail on threading more carefully....sorry to say that, but it seems that you copied the whole code without understanding the basics of multithreading. Good study.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic