Gamestudio Links
Zorro Links
Newest Posts
Newbie Questions
by fairtrader. 12/06/23 11:29
Zorro Trader GPT
by TipmyPip. 12/04/23 11:34
Square root rule
by Smallz. 12/02/23 09:15
RTest not found error
by TipmyPip. 12/01/23 21:43
neural function for Python to [Train]
by TipmyPip. 12/01/23 14:47
Xor Memory Problem.
by TipmyPip. 11/28/23 14:23
Training with command line parameters
by TipmyPip. 11/26/23 08:42
Combine USD & BTC Pairs In Asset Loop
by TipmyPip. 11/26/23 08:30
AUM Magazine
Latest Screens
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Tactics of World War I
Who's Online Now
7 registered members (fairtrader, Quad, miwok, Martin_HH, AndrewAMD, alibaba, dpn), 581 guests, and 0 spiders.
Key: Admin, Global Mod, Mod
Newest Members
fairtrader, hus, Vurtis, Harry5, KelvinC
19019 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
Page 1 of 2 1 2
Some Java-Programmers online? Multithreading-Question #207460
05/19/08 20:26
05/19/08 20:26
Joined: Feb 2005
Posts: 3,687
Hessen, Germany
T
Tempelbauer Offline OP
Expert
Tempelbauer  Offline OP
Expert
T

Joined: Feb 2005
Posts: 3,687
Hessen, Germany
hi

i´m writing a java-application. this app has to solve a big problem. so i want to part it in multiple threads to speed up the work.
the program have to work with a file, passed by the user. my plan is to parse the file seperately (for example: the first half in thread 1 and the second half in thread2)

procedure:
parsing user input (check if all inputs are done)
prepare data for multithreadung (calculating the start and end byte of the file for every thread)
- thread1: do complex calculating (from byte 0 till x)
- thread2: do complex calculating (from byte x+1 till endoffile)
resume, if both threads are finished with its work
do conclusion tasks


so, i have a management-class. it controls the threading:

public class m
{
public synchronized void calculate()
{
//prepare data for multithreading

//start threads with the data

try{
wait();
} catch(IOException e) {}

try{
wait();
} catch(IOException e) {}

}
}

the class starts the threads. and _should_ wait till both are finished. i debugged it: the threads are started and do they work correctly. after doing it, the last command of every threads is the notifyAll()-method to tell the management class it can continue its work. but after reaching the first wait() it does not awakening. no notifyAll() of any thread can wake it up. but why?

does i get a deadlock? (the management-class is already a thread, created by the GUI of my program to avoid blocking it)
if yes, how can i solve it? i cant imageing how to solve the problem...


i´m new to java, please help me. thanks

Last edited by Tempelbauer; 05/19/08 20:26.
Re: Some Java-Programmers online? Multithreading-Question [Re: Tempelbauer] #207506
05/20/08 09:30
05/20/08 09:30
Joined: Aug 2003
Posts: 2,122
Berlin, Germany
checkbutton Offline

Expert
checkbutton  Offline

Expert

Joined: Aug 2003
Posts: 2,122
Berlin, Germany
Whole package would be useful.


I don't have a homepage, for god's sake!
Re: Some Java-Programmers online? Multithreading-Question [Re: checkbutton] #207523
05/20/08 13:15
05/20/08 13:15
Joined: Jan 2002
Posts: 4,225
Germany / Essen
Uhrwerk Offline
Expert
Uhrwerk  Offline
Expert

Joined: Jan 2002
Posts: 4,225
Germany / Essen
You're giving only very few informaion, so this is hard to guess. Remember, that when you write "wait();" that is the same like "this.wait();" and therefore the current (this) object will be used as the monitor. Be sure to call the notify() or notifyAll() method on the same object, in this case the same instance of the class m. Otherwise the wait() will never be left. Be aware that "wait()" throws an InterruptedException and catch this exception in a catch block. This catch block is usually left empty.

In general reading a file from two differen threads is not an ideal design idea. Maybe you should first read the file, then distribute its content to other threads.


Always learn from history, to be sure you make the same mistakes again...
Re: Some Java-Programmers online? Multithreading-Question [Re: Uhrwerk] #207560
05/20/08 18:52
05/20/08 18:52
Joined: Feb 2005
Posts: 3,687
Hessen, Germany
T
Tempelbauer Offline OP
Expert
Tempelbauer  Offline OP
Expert
T

Joined: Feb 2005
Posts: 3,687
Hessen, Germany
Quote:
e sure to call the notify() or notifyAll() method on the same object,

really? the same object? hmm.. there is the mistake...
in class m i start new threads of another class.

Quote:
In general reading a file from two differen threads is not an ideal design idea. Maybe you should first read the file, then distribute its content to other threads.

ok, but what is if the file is very big (like 2 or 3 gigabytes)? load all data into the program would´nt fit into the ram
i want to adapt my application for small also for very big files. is there another possibility how to design the management of the data?


thanks, for the post smile


[edit]
before i forgot: if i want to start new threads that works with the read data, i have to create new objects (new objects of a class that extends the Thread-class and overwrites the run method). so how can i call the notify-method in the same object when its status is waiting for a notification? i dont understand...

Last edited by Tempelbauer; 05/20/08 18:56.
Re: Some Java-Programmers online? Multithreading-Question [Re: Tempelbauer] #207573
05/20/08 21:17
05/20/08 21:17
Joined: Sep 2004
Posts: 130
Italy
Alessandro Offline
Member
Alessandro  Offline
Member

Joined: Sep 2004
Posts: 130
Italy
Hello, I wish to give you a hint: generally speaking (not only related to java) threading is a good procedure if:

1) threads make different jobs with big delay (idle time). For example, instead analyzing data with both threads, you could create one thread to read data from hdd to an internal buffer (long-time process), then another thread analyze the data. You could use a double-buffering or triple-buffering tecnique:

Thread(1): read data and store in buffer 1 (slow)
Thread(2): read data and store in buffer 2 (slow)
Thread(3): jumps from one buffer to another one to process data (fast)

2) You are sure you can use multiple processors (thread(1)->CPU_1; thread(2)->CPU_2; etc...)

Else, using multithreading will make you take more time than a single threading. In fact, multithreading usage is very useful in particular cases (see item (1)).
If you use more threads and you cannot follow one of the previous points, I think your program could be even slower, not faster!

(multithreading is a powerful but dangerous tool!).

@Tempelbauer, about your EDIT: are you using interface Runnable or are extending Thread class?


Last edited by Alessandro; 05/20/08 21:19. Reason: more info
Re: Some Java-Programmers online? Multithreading-Question [Re: Alessandro] #207579
05/20/08 21:45
05/20/08 21:45
Joined: Jan 2002
Posts: 4,225
Germany / Essen
Uhrwerk Offline
Expert
Uhrwerk  Offline
Expert

Joined: Jan 2002
Posts: 4,225
Germany / Essen
In most cases when you use wait() and notify() you have a producer, a consumer and some kind of storage object, like a piped queue. Now what you want to do is synchronize the access of the producer and the consumer on the storage object. Additionally the consumer is expected to wait for the producer to fill in something into the storage.

What you want to do is letting the main thread wait until the two other threads are finished with their work. In this case you should use the join() method. join() stops the currently active thread as long as the thread whos join mehtod you called is done.

Code:
MyThread t1 = new MyThread();
MyThread t2 = new MyThread();
t1.start();
t2.start();
t1.join();
// Now you can be sure t1 is not running any more.
if (t2.isAlive())
	t2.join();
// Now both threads are done.

Keep in mind that now your're main thread is not doing anything at all except for waiting for the other two new threads. Allesandros suggestion of having one thread reading data into a buffer and another one working with the data might be a good alternative. Reading from a hard disk is somehting that wastes a lot of time. Maybe you can create a consumer producer example like mentioned above with this idea.


Always learn from history, to be sure you make the same mistakes again...
Re: Some Java-Programmers online? Multithreading-Question [Re: Uhrwerk] #207618
05/21/08 07:27
05/21/08 07:27
Joined: Feb 2005
Posts: 3,687
Hessen, Germany
T
Tempelbauer Offline OP
Expert
Tempelbauer  Offline OP
Expert
T

Joined: Feb 2005
Posts: 3,687
Hessen, Germany
i can follow your argumentation that the hard disk would slow down my program...
interesting, i havent mention the method join. could be useful

but what if i use 1 thread to read and analyse the data instead of having 2 (consumer and producer)? would it be faster? or is the consumer-procuder-technique the best way?

Quote:
@Tempelbauer, about your EDIT: are you using interface Runnable or are extending Thread class?

the runnable, yes. my mistake...

Re: Some Java-Programmers online? Multithreading-Question [Re: Tempelbauer] #207676
05/21/08 16:37
05/21/08 16:37
Joined: Jan 2002
Posts: 4,225
Germany / Essen
Uhrwerk Offline
Expert
Uhrwerk  Offline
Expert

Joined: Jan 2002
Posts: 4,225
Germany / Essen
Since version 1.1 Java supports threads on the operation system level (at least on windows and linux I am sure about that). So using multiple threads will heavily increase the performance on multicore processors. Even if you have a single core processor the speed will increase. The thread reading from harddisk can read any amount of data (only limited by the size of the buffer) without having to wait for the processing of the data. The processing thread can do it's work without having to wait for the data to be read from the harddisk. So the waiting time for IO operation is minimized to almost zero. If reading the data from the disk is much faster than the processing you might want to assign a lower priority to the hd reading thread. Using a BufferedInputStream to wrap the FileInputStream can additionally increase the performance heavily, especially if you need only small amounts of data at a time, like bytes or processor words.


Always learn from history, to be sure you make the same mistakes again...
Re: Some Java-Programmers online? Multithreading-Question [Re: Uhrwerk] #207710
05/21/08 19:50
05/21/08 19:50
Joined: Feb 2005
Posts: 3,687
Hessen, Germany
T
Tempelbauer Offline OP
Expert
Tempelbauer  Offline OP
Expert
T

Joined: Feb 2005
Posts: 3,687
Hessen, Germany
ok, thanks for the information smile

one more question about Swing (i dont want to open a new topic):

i´ve put a JTextArea into a JScrollPane. Both are placed in a Tab of a JTabbedPane.
if i type in (multiline) information the scrollbar works perfectly. but if i change to another tab the tabbedpane resizes to the typed in lines in the textarea. but thats very bad. how can i disable this resizing?

Re: Some Java-Programmers online? Multithreading-Question [Re: Tempelbauer] #207733
05/21/08 23:41
05/21/08 23:41
Joined: Jan 2002
Posts: 4,225
Germany / Essen
Uhrwerk Offline
Expert
Uhrwerk  Offline
Expert

Joined: Jan 2002
Posts: 4,225
Germany / Essen
I'd place a bet on the setPreferredSize() method. But once again you're providing to less information. wink What layout manager do you use?

(A great IDE for rapidly developing user interfaces for java is the Visual Editor built into eclipse. In general eclipse can be recommended for almost any java development. It's a really great and very powerfull tool, that saves you a lot of time.)


Always learn from history, to be sure you make the same mistakes again...
Page 1 of 2 1 2

Gamestudio download | chip programmers | Zorro platform | shop | Data Protection Policy

oP group Germany GmbH | Birkenstr. 25-27 | 63549 Ronneburg / Germany | info (at) opgroup.de

Powered by UBB.threads™ PHP Forum Software 7.7.1