Thread, synchronized, wait(), notify, notifyall

Posted on 2006-05-30
Last Modified: 2010-05-18
could u plz explain what these concept means in a very short way....and give some example where it is possible to use...

why we have threads in programming????

what mean thread??

what mean synchronizd??

what mean locks, notify and notifyall..

Question by:Maggieshah
    LVL 86

    Expert Comment

    That's no possible to summarize shortly. I suggest
    LVL 92

    Expert Comment


    > why we have threads in programming????

    When you want/need to do more than one thing at the same time

    > what mean thread??

    a thread is single thread of execution

    > what mean synchronizd??

    means a block of code that is guaranteed to only be executed by a single thread at one time
    other threads will wait until it is com[plete before executing

    > what mean locks, notify and notifyall..

    they are for controlling thread execution
    when a thread can hold a lock on a object, and also notify other threads when locks are freed etc.


    Author Comment

    what mean deadlock??
    LVL 92

    Expert Comment

    when thread A is waiting for a lock help be thread B
    But thread B is also waiting on a lock held by thread A
    so both qare stuck
    LVL 24

    Accepted Solution

    My point of view:

    >> why we have threads in programming????
    Only for programs that must perform several tasks in parallel, but mostly
    for programs that must control multiple external events.
    By dividing a program in threads one part of the program may wait for something while
    another part goes on with some processing.
    Formerly we did such a thing by 'frequently looking if something happened' but
    the more frequently you look the more computer time is wasted.
    A good example is a program that continues some processing, but is still
    controlled by some button(s), e.g. an abort button.

    >> what mean thread??
    A single stream of instructions performed. When it ends (or returns) it is dead.
    When all threads of a program are dead, the program is dead.

    >> what mean synchronizd??
    Co-operation between 2 or more threads so that they don't damage common data.
    Image 2 programmers editing the same file: one must wait for the other's save and exit.
    When one or more threads 'wait' for actions of another thread to complete,
    we call the operation synchronized.
    The keyword 'synchonized' in JAVA automatically invokes waiting of other threads.

    >> what mean locks, notify and notifyall..
    A lock is a way to make threads wait; it can be a variable or such;
    notify is an action to wake-up a waiting thread;
    notifyAll is an action to wake-up all threads that may wait for something.

    >> what mean deadlock??
    As 'objects' described: 2 or more threads waiting for each other (eternally).
    It should of course never happen.

    LVL 3

    Expert Comment

    LVL 14

    Expert Comment

    > and give some example where it is possible to use
    consider you are writing a client/server chat system. the server must be able to deal with more than one client at a time. The way of dealing with this in Java is by multithreading. a thread is a task, or process, that may run in parallel with other processes. simply a thread is a program's path of execution. Multithreaded applications deliver their potent power by running many threads concurrently within a single program. From a logical point of view, multithreading means multiple lines of a single program can be executed at the same time. so in my example, the multithreaded chat server waits for a connection, when a connection is made, a new thread will be created and then looks after that connection. The server then waits for another connection while the client threads are listening to each client's request concurrently.
    LVL 30

    Expert Comment

    >> why we have threads in programming????

    Consider a case of a simple web-server like Yahoo or experts-exchange. There is a web-application running there which 100s of users are accessing. So the web-server process needs to handle all of these requests 'parallelly', hence in different threads simultaneously so that it doesn't make one user wait until it has entirely handled the other user.

    Featured Post

    What Should I Do With This Threat Intelligence?

    Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    maxMirror challenge 10 68
    Receive file in Servlet 1 28
    IT Company 5 56
    Which is the latest version of Apache Tomcatl 3 20
    Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
    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 if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
    This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

    728 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

    20 Experts available now in Live!

    Get 1:1 Help Now