Hello everyone,
Can someone please explain to me the meaning of the code below:

public class Client extends javax.swing.JFrame implements Runnable {

// constructutor
public Client( String host, int port ) {
       .
       .
       .
 new Thread( this ).start();

}

public void run() {
something....
}
}

This is the only part I have not understand:

new Thread( this ).start();

What does the "this" represents?
What is new Thread?
.Start() will run what?
Thread( this ) what is the type of his here and where it goes?

Thanks in advance.

Recommended Answers

All 12 Replies

this represents the object whose method is currently executing.
A Thread is a separate stream of execution (eg if you have a dual processor, you can execute two threads simultaneously).
Your class implements the Runnable interface, which consists simply of a public void run() method.
So new Thread(this) creates a new thread in which code from your current object ("this") can be run. The start() method sets the thread running by calling the run() method in your current object.
Whatever is in the run() method will start to execute in parallel with whatever follows the new Thread(this).start(); statement. You now have two threads running which can continue to execute (or wait, or whatever) independently.

Thanks for the answer James.

But what happen if i run the program on a single core processor? Another question:

Can you please explain the code that represent "this" in the above example.

does

new Thread(this).start () equivalent of new Thread(Client).start() ?

You said this represents "code from your current object ", isn't it ? what is the code for the current object, its the constructors, methods or both constructors and methods?

Thanks a lot for the fast reply.

If you have a single processor the two (or more) threads will share it according to some process that you don't get involved with. In particular, if one thread has to wait (eg for user input) the other will be able to continue running.

"this" represents the current instance of the class. In your example new Thread(Client) would try to use the class itself, not the current instance.
Whenever you run an ordinary (not static) method, you have to specify the object that it will run for eg myString.toUpperCase(); the toUpperCase method is running for the object myString. During the execution of toUpperCase, the code could refer to "this", which in the example would be myString. If you then called
myOtherString.toUpperCase(), "this" in the toUpperCase method would now refer to myOtherString.
In a constructor "this" refers to the object that's being constructed.
I don't know if you can use "this" in a static method - if you can it would presumably refer to the class iteself (?).

I understood now !!

I got one last question on synchronisation, I think its better to start it in a new thread.

Hello, I got one last. I don't know if you remember me, but I was developing a chat system. And at the beginning my application was freezing because of the while loop () when the server listen.

You told me to use thread and everything works pretty fine. The code above in another way to solve the freezing situation, isn't it?

That's we could put the while in the run() and when the JVM reaches

new Thread( this ).start();

it will run the while loop in a new thread, isn't it?


Its not a question but just to confirm if i am right !!

Thanks a lot for all your help dude. J

Ah, now I see your question!
Yes, the new thread means that the while loop can wait for connections in its thread while the user interface continues to do its stuff in its own thread. You don't necessarily need to use synchronised - in the case above you probably don't. You add a synchronised statement when you determine that there is a danger of two threads trying to access the same object at the same time in an unsafe way (eg both updating it - if they're both just reading it it's probably not a problem).

Ok, Network programming in Java is so interesting. Thanks for the info. Thanks a lot for your help James.

NeutralFox - I'm glad you solved your problem - James is correct in saying "If you have a single processor the two (or more) threads will share it according to some process that you don't get involved with". As an example of that, consider the fact that you can use your email, play music, and IM all at the same time.

Okie .. Hello BJSJC !! I got a quad core extreme at home !! If I used thread in my program it should works faster on my PC but its just a small chat application, i don't think that I will see the difference on a quad or on a single core computer.

Anyway, thanks for the answer.

Yes, you are correct - you would not notice a difference. However, if you started playing games, chatting, listening to music, and downloading files off the web, you might notice a difference between a single core and quad core machine. Also, FYI, each time you start a Java application, the main method is run in a new thread.

Thanks for the info dude.

I got some more questions ... lol, a bit tired today, tomorrow I will post a new thread.

A+

Yes, the reason for multiple threads in Java is very rarely for performance , it's usually to allow the UI the run while something else is happening in the background, or to support multiple remote access sessions simultaneously. As BJSJC says you always get a new thread for your main(...) method at startup, but also Swing starts its own new thread for everything to do with the UI (ie screen painting, responding to mouse clicks etc).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.