Click here to Skip to main content
15,886,873 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
In this code, I give the thread b (that prints b) the highest priority, but when I run the program still ends with a series of "b". What is the reason for this? This code is made by my Prof software, but he doesn't know the answer as well. It is only used to show how multithreading is used.

The threads given by Java are used here.

public class PrintThread6 extends Thread 
    {
        String message;
        public PrintThread6 (String m)
        {
            message = m;
        }
    
        public void run ()
        {
            for (int i = 0; i<10000; i++)
            {
    //  /*
                try
                {
                    Thread.sleep(1);
                }
                catch(InterruptedException e)
                {
                    System.out.println("Thread interrupted in sleep");
                }
    //  */
                System.out.print(message);
            }
        }
    
        public static void main (String args[ ]) 
        {
            try
            {
                Thread.sleep(0);
            }
            catch(InterruptedException e)
            {
                System.out.println("Main interrupted before creating threads");
            }
    
            Thread a = new PrintThread6("a");   Thread b = new PrintThread6("b");
            Thread c = new PrintThread6("c");   Thread d = new PrintThread6("d");
            Thread e = new PrintThread6("e");   Thread f = new PrintThread6("f");
            Thread g = new PrintThread6("g");   Thread h = new PrintThread6("h");
            Thread i = new PrintThread6("i");   Thread j = new PrintThread6("j");
            Thread k = new PrintThread6("k");   Thread l = new PrintThread6("l");
            Thread m = new PrintThread6("m");   Thread n = new PrintThread6("n");
            Thread o = new PrintThread6("o");   Thread p = new PrintThread6("p");
            Thread q = new PrintThread6("q");   Thread r = new PrintThread6("r");
            Thread s = new PrintThread6("s");   Thread t = new PrintThread6("t");
            Thread u = new PrintThread6("u");   Thread v = new PrintThread6("v");
            Thread w = new PrintThread6("w");   Thread x = new PrintThread6("x");
            Thread y = new PrintThread6("y");   Thread z = new PrintThread6("z");
    
            a.setPriority(3);
            b.setPriority(7);
            System.out.println("Priority of main thread: " + Thread.currentThread().getPriority() +
                               ", thread a: " + a.getPriority() +
                               ", thread b: " + b.getPriority());
    
            a.start();  b.start();  c.start();  d.start();  e.start();  f.start();
            g.start();  h.start();  i.start();  j.start();  k.start();  l.start();
            m.start();  n.start();  o.start();  p.start();  q.start();  r.start();
            s.start();  t.start();  u.start();  v.start();  w.start();  x.start();
            y.start();  z.start(); 
    
            int active = Thread.activeCount();
    
            System.out.print("c");
    
            try
            {
                a.join();  b.join();  c.join();  d.join();  e.join();  f.join();
                g.join();  h.join();  i.join();  j.join();  k.join();  l.join();
                m.join();  n.join();  o.join();  p.join();  q.join();  r.join();
                s.join();  t.join();  u.join();  v.join();  w.join();  x.join();
                y.join();  z.join(); 
            }
            catch (InterruptedException ie)
            {
                System.out.println("Main interrupted whilst joining threads");
            }
            System.out.println("c");
            System.out.println("\nActive threads: " + active);
        }
    }


What I have tried:

Asking my dorm mate. He answered this: k denk dat het komt omdat je niet alle threads tegelijk kan runnen, en telkens je thread sleep opropet wordt hij in de wacht gezet. maar dan krijgen de threads van de volgende hoogste (of zelfde, maar die zijn er niet) prioriteit voorrang. Ik denk dat daar iets misloopt , zodat hij eerst een paar lagere priority threads doet.
Posted
Updated 13-Nov-19 6:47am

1 solution

Probably because of the number of threads and the waits created by calls to System.out.print. Remember a high priority thread will only be selected for execution when it is waiting, and no higher priority thread is waiting. But if thread b has just called System.out then it is in a wait state and any of the other threads can be run. Thread b will not be considered for execution again until all active threads relinquish control.

In short, you cannot control which thread runs at any point in time. You can only ask the system to give priority when possible. And with 26 threads running your application will not be very efficient.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900