Solved

Some question about Thread

Posted on 2003-11-28
11
251 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
  • 4
  • 2
  • 2
  • +2
11 Comments
 
LVL 3

Assisted Solution

by:applekanna
applekanna earned 50 total points
Comment Utility
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
Comment Utility
checked, but still not quite clear about it.
0
 
LVL 3

Expert Comment

by:applekanna
Comment Utility
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
 
LVL 3

Expert Comment

by:applekanna
Comment Utility
0
 

Author Comment

by:JAVAnewbie
Comment Utility
;)

i will definitely wait til all my question being answered then i allocate my pts.. ;)
tks so very much!
0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 100 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Thx for the points :)
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
:-)
0
 
LVL 15

Expert Comment

by:Javatm
Comment Utility
:) Glad to help . . .
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
bigHeights  challenge 13 55
countAbc challenge 9 49
wordlen challenge 3 46
thymeleaf natural templating vs JSP 2 23
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

772 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now