473,386 Members | 1,694 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

How about to lauch a new thread in a constructor?

I read one piece of code written by somebody and behold interesting
class as below:

class SomeFramework {
public SomeFramework() {
...
DameonThread dameon = new DameonThread();
dameon.start();
}

private doSomething() {
...
}

private class DameonThread extends Thread {
public void run() {
...
doSomething();
...
}
}
}

As you can see, in the constructor of SomeFramework, one thread is
launched and that thread will call the SomeFramework instance's private
method. Perhaps the author design this code to make the client code
easy. The client just call "new SomeFramework()" and everything is done.
No need to coding like
SomeFramwork framework = new SomeFramework();
framework.lauch();

However, the problem is that it is possible that when the dameon is
started, CPU will go into run and call doSomething() of the instance of
SomeFramework. But, at that time, the constructor is not returned. Is it
reasonable to call a method of a un-instanstized object? Curiously, this
code works well all the time. Luckily, before dameon is started, all
member variables are instantized in constructor, but I really think this
code is ugly and should be avoided.
Jul 17 '05 #1
0 1669

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

Similar topics

4
by: Leonardo Hyppolito | last post by:
Hello, I am trying to write a multithread program that simulates producers and consumers. My program can have many producers and many consumers (each in a separate thread). It has a storage...
12
by: Ricardo Pereira | last post by:
Hello all, I have a C# class (in this example, called A) that, in its constructor, starts a thread with a method of its own. That thread will be used to continuously check for one of its...
3
by: Bob Day | last post by:
VS 2003, vb.net, sql msde... The help is pretty empatic that you cannot pass parameters to a thread. The sample below is from help, showing you to set up variables in the TasksClass, and assign...
9
by: esakal | last post by:
Hello, I'm programming an application based on CAB infrastructure in the client side (c# .net 2005) Since my application must be sequencally, i wrote all the code in the UI thread. my...
4
by: jayesah | last post by:
Hi All, I am writting a Thread class with using pthread library. I have some problem in saving thread function type and argument type. How to make Thread class generic ? /* This is my global...
6
by: HolyShea | last post by:
All, Not sure if this is possible or not - I've created a class which performs an asynchronous operation and provides notification when the operation is complete. I'd like the notification to be...
8
by: Lee Alexander | last post by:
I wrote some code in a method called in a separate thread via the ThreadPool where I expected my code in a Finally section to be called but it never did. I'd be interested to know why this is the...
6
by: Sergey Poberezovskiy | last post by:
I have the following code in C# that I have trouble converting to VB(2.0): private delegate void openDialog(); private void openWindowsDialog(openDialog open) { Thread thread = new Thread(new...
0
by: rahulsengar | last post by:
Hey how can i lauch my application on incoming call notification in windows mobile . hey can u please help me how to launch my application automatically when an incoming call occurs. Is there any...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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,...
0
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...

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.