Solved

Some question about Thread

Posted on 2003-11-28
11
256 Views
Last Modified: 2010-05-18
I have come across the java tutorial at:
http://java.sun.com/docs/books/tutorial/essential/threads/

and i was confused... and i hope experts here can give me a clear explanation about:

1) Thread
i know what is a thread, but how can we use it?

2) Runnable
what is runnable? how to use it?

3) Timer
how to use? what is does? and most importantly, what is it?

4) TimerTask
how to use? what is does? and most importantly, what is it?

5) i think i've seen
 Thread aThread = new Thread(new Runnable(){...});
before..(not quite sure the real code).. what it does?

6) what is synchronization? how to "use" it? or make sth synchronize?

*My English is very bad, so, please make your explanation simple, with example...

Tks

PS: i allocate 50 pts for each answer, totalling to 300.

0
Comment
Question by:JAVAnewbie
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
  • 2
  • +2
11 Comments
 
LVL 3

Assisted Solution

by:applekanna
applekanna earned 50 total points
ID: 9836450
what is a thread and how we can use it
check this thread.

http://www.experts-exchange.com/Programming/Programming_Languages/Java/Q_20804777.html
0
 

Author Comment

by:JAVAnewbie
ID: 9836560
checked, but still not quite clear about it.
0
 
LVL 3

Expert Comment

by:applekanna
ID: 9836635
a thread is a process.

it is something like this,

u go to a gas station and ask someone to fill the gas while you go inisde and have a beer.
u do this because filling gas and drinking beer and diffrent process and do not affect each other and can be done in any order.

the act of calling someoneFillGas() is writen in a thread

somefileGas will be in class that extends threads and has a run() method

try this
http://www.ecs.umass.edu/ece/wireless/people/emmanuel/java/java/threads/simple.html

Hope this helps.
Cheers
0
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 
LVL 3

Expert Comment

by:applekanna
ID: 9836697
0
 

Author Comment

by:JAVAnewbie
ID: 9836765
;)

i will definitely wait til all my question being answered then i allocate my pts.. ;)
tks so very much!
0
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 100 total points
ID: 9837357
1. Consider that answered by applekanna

2. Runnable is an interface that is intended to separate the logic of what's done in its method - public void run() - and allocating the means to run it (creating a Thread object). The way this is done is exactly as you said below. A Thread is created which runs whatever it finds in the Runnable's run method.

>>
i think i've seen
 Thread aThread = new Thread(new Runnable(){...});
before..(not quite sure the real code).. what it does?
>>

3. A Timer performs a task at predefined intervals.

4. A TimerTask is the bit that does the business of whatever the Timer is timing. A Thread is created and the Runnable (the TimerTask *is* a Runnable) is executed. See
http://javaalmanac.com/egs/java.util/ScheduleRepeat.html

5. See 2.

6. Synchronization is about making sure two or more threads are accessing the same resource in an orderly fashion. Otherwise the value of variables can become corrupted or the resources state can become indeterminate. See http://developer.java.sun.com/developer/TechTips/2000/tt0328.html#tip2
0
 
LVL 15

Assisted Solution

by:dualsoul
dualsoul earned 75 total points
ID: 9837401
1) Thread
i know what is a thread, but how can we use it

this is the class for manipulating your threads in Java
you can subclass it and rewite run() method - here you should place your code to execute in separate thread

2) Runnable
what is runnable? how to use it

altenative inteface for thread, if you cant't subclass Thread (possibly subclassing another one class), you can implement thi inteface, it has the same method run()

) i think i've seen
 Thread aThread = new Thread(new Runnable(){...});

if do point 2) you can write thread like this:
     new Thread(new MyRunnableIml()).start();
if point 1): then just new Mythead().start();
so after start you run() code will execute in separate thead concurently.

6) what is synchronization? how to "use" it? or make sth synchronize?

synchronization is the mechanism two exclude situation where 2 or more threads running the same pieace of code at the same time.
you can do it by:
  synchonized methods:  public synchronized void operatio()
  synchonized blocks of code:
          synchronized{
          //so code here
         }



 
0
 
LVL 15

Accepted Solution

by:
Javatm earned 75 total points
ID: 9840350
Here's my share friend :

>> I know what is a thread, but how can we use it ?

     Let me give you a sample : " Swing ";

     Swing is a single threaded package, If you wanna to add many capabilities like printing,
     opening-up and saving of documents on your swing program you might consider using
     thread and set-up a multi threading on the program besides Java is multi threaded.

     Why ? It is beacuse if you try to run printing, saving & opening of documents on swing
     it might distort the image of the swing components while your doing some operations.

     Right now I'm trieng to make my own Java IDE and I have proven that multi-threading
     is one of the best solution for my program not to be distorted whil its doing its printing
     opening and saving of documents.

     Thanks to CEHJ;

>> what is runnable? how to use it?

      Runnable is a needed to run a thread, it is a control that which thread tries to base
      when its gonna run depending on how you do it.

>> Timer
      how to use? what is does? and most importantly, what is it?

      A facility for threads to schedule tasks for future execution in a background thread.
      Tasks may be scheduled for one-time execution, or for repeated execution at regular intervals.

       Corresponding to each Timer object is a single background thread that is used to execute all
       of the timer's tasks, sequentially. Timer tasks should complete quickly. If a timer task takes
       excessive time to complete, it "hogs" the timer's task execution thread. This can, in turn, delay
       the execution of subsequent tasks, which may "bunch up" and execute in rapid succession when
       (and if) the offending task finally completes.

       See this :
       http://java.sun.com/docs/books/tutorial/essential/threads/timer.html

>> TimerTask
      how to use? what is does? and most importantly, what is it?

     A task that can be scheduled for one-time or repeated execution by a Timer

     See this :
     http://java.sun.com/j2se/1.4.2/docs/api/java/util/TimerTask.html

>> what is synchronization? how to "use" it? or make sth synchronize?

      See this :
      http://java.sun.com/docs/books/tutorial/essential/threads/multithreaded.html

     Here are all the information :
     http://java.sun.com/docs/books/tutorial/essential/threads/

Hope this helps . . .
JAVATM
0
 
LVL 3

Expert Comment

by:applekanna
ID: 9848988
Thx for the points :)
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9849292
:-)
0
 
LVL 15

Expert Comment

by:Javatm
ID: 9853568
:) Glad to help . . .
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
hibernate example for saving data 19 106
ejb entity bean example issue 2 40
printf performancy 11 98
IBM TS2900 (3572) Tape Autoloader Java? 12 99
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question