Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 11 of 11

Thread: Multithreading

  1. #1
    Junior Member
    Join Date
    Dec 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Multithreading

    I have a question regarding join(),setPriority() methods. How we can I see the working of these methods?. I could not get the reality in windows OS when i coded in java. Is it due to Windows is preemtive scheduling and due to this it dint behave appropriately? Ideally when join() is invoked on a thread, the current thread which has applied join() on a thread, current thread waits until the thread on which join is performed gets done but this was not the case when i ran the program.
    Example : t3.join(); t1.join(); t2.join(); This has to work as mentioned below.

    T3 thread completes its process and then T1 gets done and finally it goes with T2. But what i saw is T1, T2 and T3 got done in sequential manner which should not be.

    Any idea why it behaved so?
    Last edited by Miths; December 15th, 2012 at 09:07 PM. Reason: mistakes here and there


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,265
    Thanks
    56
    Thanked 2,372 Times in 2,343 Posts

    Default Re: Multithreading

    Can you post a small program that compiles, executes and shows what you are asking about.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Dec 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Multithreading

    Just now realized the working of join() method. But still setPriority() is a bit confusing me.

    INPUT:

    threadC.setPriority(Thread.MAX_PRIORITY);
    threadB.setPriority(threadA.getPriority() + 1);
    threadA.setPriority(Thread.MIN_PRIORITY);
    System.out.println("Started Thread A");
    threadA.start();
    System.out.println("Started Thread B");
    threadB.start();
    System.out.println("Started Thread C");
    threadC.start();
    System.out.println("End of main thread");

    OUTPUT:

    Started Thread A
    Started Thread B
    Started Thread C
    Thread B started
    Exit from B
    Thread A started
    Exit from A
    Thread C started
    Exit from C
    End of main thread

    My Expectation : Thread C should have done its task first and ThreadA at last but when i ran the program it dint work so.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,265
    Thanks
    56
    Thanked 2,372 Times in 2,343 Posts

    Default Re: Multithreading

    Can you post a small, complete program that compiles, executes and shows what you are asking about.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Dec 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Multithreading

    My Question is, The thread which is been set to MAX_PRIORITY has to be executed first and thread which has LOW_PRIORITY has to be executed last but it's not working so.

    Code:
    package first;
     
    class A extends Thread {
    	public void run() {
    		System.out.println("Thread A");
    	}
    }
     
    class B extends Thread {
    	public void run() {
    		System.out.println("Thread B");
    	}
    }
     
    class C extends Thread {
    	public void run() {
    		System.out.println("Thread C");
    	}
    }
     
    public class ThreadPriorityDemo {
    	public static void main(String args[]) {
    		A threadA = new A();
    		B threadB = new B();
    		C threadC = new C();
    		threadC.setPriority(Thread.MAX_PRIORITY);
    		threadB.setPriority(threadA.getPriority() + 1);
    		threadA.setPriority(Thread.MIN_PRIORITY);
    		threadA.start();
    		threadB.start();
    		threadC.start();
    		System.out.println("End of main thread");
    	}
    }
    OUTPUT:

    Thread A
    Thread B
    End of main thread
    Thread C
    Last edited by helloworld922; December 17th, 2012 at 01:19 PM. Reason: please use [code] tags

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,265
    Thanks
    56
    Thanked 2,372 Times in 2,343 Posts

    Default Re: Multithreading

    What do you expect the output should be and why?
    The code is trivial and will probably execute immediately when started.

    Please edit your post and wrap your code with
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Dec 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Multithreading

    ThreadC has got maximum priority which is 10, so it has to be processed at first. [ threadC.setPriority(Thread.MAX_PRIORITY); ]
    ThreadB has priority which is in between ThreadA and threadC. [ threadB.setPriority(threadA.getPriority() + 1); ]
    ThreadA is of least priority, so i expect it has to be processed at last. [ threadA.setPriority(Thread.MIN_PRIORITY); ]

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,265
    Thanks
    56
    Thanked 2,372 Times in 2,343 Posts

    Default Re: Multithreading

    ThreadC has got maximum priority which is 10, so it has to be processed at first.
    ThreadA is started first and finishes immediately. ThreadC has not been started yet.

    You need the threads to do something that takes longer so that their priority can have an effect.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Dec 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Multithreading

    Just added a for loop for each thread so that it performs some action for a while.
    class B extends Thread {
    	public void run() {
    		for(int i = 0; i<100; i++){
    		System.out.println("Thread B");}
    	}
    }

    But in this case also i see that Thread A got displayed 100 times and Thread B has displayed 100 times followed by "End of main thread" and at last displayed Thread C 100 times. Why the threads are not executing as per priority.

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,265
    Thanks
    56
    Thanked 2,372 Times in 2,343 Posts

    Default Re: Multithreading

    Try adding some calls to yield() or sleep() inside the threads.
    The tight loops in the code don't give the OS time to get control and pass control to another thread.
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Junior Member
    Join Date
    Dec 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Multithreading

    you should balance your codes....if you are placing 100 lines in class A...you should place 100 lines in class b and c to compare the results....with 1 line of coding to execute it is difficult to check for priority...

    try to insert these lines for the loop in class b and c as well...im sure the code is working....but the processing time is too quick to have a good comparative result....

Similar Threads

  1. Multithreading / Multiple Connections
    By Spicyfish in forum Java Networking
    Replies: 21
    Last Post: August 11th, 2012, 02:21 AM
  2. Multithreading Problem
    By Staticity in forum What's Wrong With My Code?
    Replies: 2
    Last Post: August 2nd, 2012, 08:39 PM
  3. Java Multithreading Example - Issues
    By vijayinani in forum What's Wrong With My Code?
    Replies: 3
    Last Post: May 30th, 2012, 11:46 AM
  4. Multithreading in java
    By skillet in forum Java Theory & Questions
    Replies: 1
    Last Post: March 17th, 2012, 08:32 AM
  5. Multithreading and its importance in java
    By jessie143143 in forum The Cafe
    Replies: 0
    Last Post: October 13th, 2011, 03:09 PM