473,799 Members | 2,941 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

thread priority question...

JDK 1.4 on WinXP. I have 2 threads started from the main thread. I would
like to print some status of the child threads from the main thread
periodically. But I dont see the main thread to print out anything when the
child threads are running. The child threads do sleep after some work.
Instead should I make the child threads wait() and make the main thread
notifyAll() after printing ?

public class MainThread {
WorkerThread t1, t2;
public static void main(String[] args) {
new MainThread().st art();
}

public void start() {
t1 = new WorkerThread();
t2 = new WorkerThread();
t1.start();
t2.start();
// even this is not printed...
System.out.prin tln("print");
printWorkerStat us();
}

public void printWorkerStat us() {
while ( t1.isAlive() || t2.isAlive() ) {
t1.printStatus( ); // this print never happens
t2.printStatus( ); // this print never happens
Thread.sleep(40 *1000);
}
}
}
Mar 8 '06 #1
6 6241
Padhu Vinirs wrote:
public void printWorkerStat us() {
while ( t1.isAlive() || t2.isAlive() ) {
t1.printStatus( ); // this print never happens
t2.printStatus( ); // this print never happens
Thread.sleep(40 *1000);
}


This code will not compile. Thread.sleep method throws an
InterruptedExce ption which is not handeled anywhere. Fix it. The rest of
the code seems ok. Should you have more problems, post the content of
the printStatus method.

Hope this helps

Marek

--
# You can't run away. Everyone's connected.
# Marek Puchalski
# Proud linux user: 409592
Mar 8 '06 #2

This was sample code I typed in here. Please assume that the exceptions are
caught etc. printStatus is very simple:

public void printStatus() {
// some system.out of thread's state variables...
}

Thanks

-- padhu

"Marek Puchalski" <ma**********@g mail.com> wrote in message
news:du******** ***@node3.news. atman.pl...
Padhu Vinirs wrote:
public void printWorkerStat us() {
while ( t1.isAlive() || t2.isAlive() ) {
t1.printStatus( ); // this print never happens
t2.printStatus( ); // this print never happens
Thread.sleep(40 *1000);
}


This code will not compile. Thread.sleep method throws an
InterruptedExce ption which is not handeled anywhere. Fix it. The rest of
the code seems ok. Should you have more problems, post the content of the
printStatus method.

Hope this helps

Marek

--
# You can't run away. Everyone's connected.
# Marek Puchalski
# Proud linux user: 409592

Mar 8 '06 #3
Padhu Vinirs wrote:
This was sample code I typed in here. Please assume that the exceptions are
caught etc. printStatus is very simple:

public void printStatus() {
// some system.out of thread's state variables...
}


It's a pain to try to find bugs in a sample code. See this. It works fine.

class WorkerThread extends Thread
{
public void run()
{
while ( true )
;
}

public void printStatus()
{
System.out.prin tln( 111 );
}
}

public class MainThread
{
WorkerThread t1, t2;

public static void main( String[] args )
{
new MainThread().st art();
}

public void start()
{
t1 = new WorkerThread();
t2 = new WorkerThread();
t1.start();
t2.start();
// even this is not printed...
System.out.prin tln( "print" );
printWorkerStat us();
}

public void printWorkerStat us()
{
while ( t1.isAlive() || t2.isAlive() )
{
t1.printStatus( ); // this print never happens
t2.printStatus( ); // this print never happens
try
{
Thread.sleep( 40 * 1000 );
}
catch ( InterruptedExce ption e )
{
// TODO Auto-generated catch block
e.printStackTra ce();
}
}
}
}

and the output is like:

print
111
111
111
111 (...)

Hope this helps.

Marek

--
# You can't run away. Everyone's connected.
# Marek Puchalski
# Proud linux user: 409592
Mar 8 '06 #4

"Padhu Vinirs" <pr*********@co mcast.net> wrote in message
news:sO******** ************@co mcast.com...

This was sample code I typed in here. Please assume that the exceptions
are caught etc. printStatus is very simple:

public void printStatus() {
// some system.out of thread's state variables...
}

Thanks


If I assume you've done everything correctly, then yes, your code works
perfectly. Since it's not working perfectly, perhaps the assumption that
you've done everything correctly is invalid.

Please post an SSCCE. http://mindprod.com/jgloss/sscce.html

- Oliver

Mar 8 '06 #5
The MainThread is not properly coded to be a Thread - it does not
impletement the java.lang.Runna ble interface. If we assume you coded the
WorkerThread the same way your program executes sequentially and therefore
you will see your output from the main "thread" as soon as the child
"threads" complete.

Regards,
Dobromir

"Padhu Vinirs" <pr*********@co mcast.net> wrote in message
news:Cu******** ************@co mcast.com...
JDK 1.4 on WinXP. I have 2 threads started from the main thread. I would
like to print some status of the child threads from the main thread
periodically. But I dont see the main thread to print out anything when
the child threads are running. The child threads do sleep after some work.
Instead should I make the child threads wait() and make the main thread
notifyAll() after printing ?

public class MainThread {
WorkerThread t1, t2;
public static void main(String[] args) {
new MainThread().st art();
}

public void start() {
t1 = new WorkerThread();
t2 = new WorkerThread();
t1.start();
t2.start();
// even this is not printed...
System.out.prin tln("print");
printWorkerStat us();
}

public void printWorkerStat us() {
while ( t1.isAlive() || t2.isAlive() ) {
t1.printStatus( ); // this print never happens
t2.printStatus( ); // this print never happens
Thread.sleep(40 *1000);
}
}
}

Mar 9 '06 #6
The main thread is the starting class ( with the main method ). That is the
default thread. It doesnt need to implement Runnable. My question was
related to the main thread being able to print status of threads it started.

-- padhu

"Dobromir Gaydarov" <no***@sympatic o.ca> wrote in message
news:u7******** ***********@new s20.bellglobal. com...
The MainThread is not properly coded to be a Thread - it does not
impletement the java.lang.Runna ble interface. If we assume you coded the
WorkerThread the same way your program executes sequentially and therefore
you will see your output from the main "thread" as soon as the child
"threads" complete.

Regards,
Dobromir

"Padhu Vinirs" <pr*********@co mcast.net> wrote in message
news:Cu******** ************@co mcast.com...
JDK 1.4 on WinXP. I have 2 threads started from the main thread. I would
like to print some status of the child threads from the main thread
periodically. But I dont see the main thread to print out anything when
the child threads are running. The child threads do sleep after some
work. Instead should I make the child threads wait() and make the main
thread notifyAll() after printing ?

public class MainThread {
WorkerThread t1, t2;
public static void main(String[] args) {
new MainThread().st art();
}

public void start() {
t1 = new WorkerThread();
t2 = new WorkerThread();
t1.start();
t2.start();
// even this is not printed...
System.out.prin tln("print");
printWorkerStat us();
}

public void printWorkerStat us() {
while ( t1.isAlive() || t2.isAlive() ) {
t1.printStatus( ); // this print never happens
t2.printStatus( ); // this print never happens
Thread.sleep(40 *1000);
}
}
}


Mar 13 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

38
2913
by: Anthony Baxter | last post by:
On behalf of the Python development team and the Python community, I'm happy to announce the release of Python 2.3.1 (final). Python 2.3.1 is a pure bug fix release of Python 2.3, released in late July. A number of obscure crash-causing bugs have been fixed, various memory leaks have been squished, but no new features have been added to the language or to the library. For more information on Python 2.3.1, including download links for...
4
8964
by: Trev Hunter | last post by:
Hi All, I was just wondering if it is safe to change thread priorities with thread pool threads (threads used by System.Threading.Timer and those used by the ThreadPool class itself). Say I have a background task that runs periodically (it uses System.Threading.Timer to do this). As the execution speed of this task is not a priority, I want to be able to set the priority of the current thread to the lowest possible. If I use
31
2513
by: AlexeiOst | last post by:
Everywhere in documentation there are recommendations to use threads from thread pooling for relatively short tasks. As I understand, fetching a page or multiple pages (sometimes up to 50 but not tipical) from the Internet and doing some processing on those would be considered to be a big/long task for a thread from a pool. In our app it is possible to break the task into some small ones (thread per fetch and processing thereafter or event...
13
7475
by: William Stacey | last post by:
Using the following code sample: public byte Get() { // <= Possible to switch Here?? lock(syncLock) { //Do something in Get(). } }
0
1174
by: bob | last post by:
I have a somewhat complex question about how Threading works across the network. Below is the full source code of a program that will list any file modified on myServer. When I run this code from myWorkstation and search myServer using a network share (using a Thread set to priority.Lowest) does myWorkstation inform myServer that this is a low priority thread? Since myServer is a production server, I don't want my file monitoring...
8
2786
by: Cider123 | last post by:
I ran into a situation where my Window Service had to process 100,000+ files, when I first noticed I needed to tweak various routines. Everything runs fine, but here's what I ran into: In the routine that loops through the file buffer: for (int i=0;i < _Buffer.length; i++) { // Code here
4
5435
by: Matthew Groch | last post by:
Hi all, I've got a server that handles a relatively high number of concurrent transactions (on the magnitude of 1000's per second). Client applications establish socket connections with the server. Data is sent and received over these connections using the asynchronous model. The server is currently in beta testing. Sporadically over the course of the day, I'll observe the thread count on the process (via perfmon) start climbing....
3
35435
by: John Nagle | last post by:
There's no way to set thread priorities within Python, is there? We have some threads that go compute-bound, and would like to reduce their priority slightly so the other operations, like accessing the database and servicing queries, aren't slowed as much. John Nagle
8
2286
by: Joe Withawk | last post by:
I have an application in which there exist a thread that handles directx rendering. It runs on a dual core system with windows vista and xp. It is set to have highest priority and simply loops with while(isRendering) { prepare for rendering; do render; present() }
9
6993
by: =?Utf-8?B?anAybXNmdA==?= | last post by:
I've got a routine that builds a table using different queries, different SQL Tables, and adding custom fields. It takes a while to run (20 - 45 seconds) so I wrote a thread to handle the table population. Whenever I call the thread, I pass it a structure containing the table and a few other parameters. The table goes in as a reference, but the other items are passed normally.
0
9686
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10475
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10250
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10222
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10026
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6805
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4139
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3757
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2938
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.