Results 1 to 4 of 4
  1. #1
    shimaa is offline Member
    Join Date
    May 2013
    Posts
    1
    Rep Power
    0

    Default Program creating two threads

    H, How are you?
    I'm totally new in Java
    and I need a full explanation of this code
    please help me
    public class NewClass implements Runnable {
    public static int counter = 0;
    public static int turn = 0;
    public static Object lock = new Object();

    @Override
    public void run() {
    while (counter < 50) {
    synchronized (lock) {
    if (turn == 0) {

    System.out.println(counter + " from thread "
    + Thread.currentThread().getName());
    turn = 1;
    try {
    lock.wait();
    } catch (InterruptedException e) {
    Thread.currentThread().interrupt();
    }
    } else {
    turn = 0;
    lock.notify();
    }

    }
    }
    }
    }
    public class NewMain {


    */
    public static void main(String[] args) {
    Thread threadA = new Thread(new NewClass ());
    Thread threadB = new Thread(new NewClass ());
    threadA.start();
    threadB.start();
    }
    }

    ---------------------
    Thank you

  2. #2
    monkeyjr97 is offline Senior Member
    Join Date
    Nov 2012
    Posts
    258
    Rep Power
    2

    Default Re: Program creating two threads

    whats the problem?

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,538
    Rep Power
    5

    Default Re: Program creating two threads

    If you need explanation, I assume you didn't write it. Correct?

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  4. #4
    allaudin's Avatar
    allaudin is offline Senior Member
    Join Date
    Apr 2013
    Posts
    271
    Rep Power
    0

    Default Re: Program creating two threads

    In fact understanding others code is most difficult thing in programming .. there is pdf guide on java thread ..please read
    http://www.d.umn.edu/~cprince/course...avaThreads.pdf

Similar Threads

  1. All Threads in Blocked State ( Program Hangs )
    By thehellmaker in forum Threads and Synchronization
    Replies: 1
    Last Post: 08-09-2011, 12:08 AM
  2. problems with threads in my program (runnable)
    By runn81 in forum Threads and Synchronization
    Replies: 12
    Last Post: 05-09-2011, 05:26 PM
  3. Creating new threads from another thread
    By enzom83 in forum Threads and Synchronization
    Replies: 1
    Last Post: 12-03-2010, 10:15 PM
  4. Using threads in a graphical program
    By jameskelly in forum New To Java
    Replies: 6
    Last Post: 11-28-2010, 03:52 PM
  5. Which method is best for creating Threads?
    By makpandian in forum Threads and Synchronization
    Replies: 11
    Last Post: 06-08-2009, 08:00 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •