Forum Stats

  • 3,727,653 Users
  • 2,245,431 Discussions
  • 7,852,906 Comments

Discussions

Thread Synchronization using Priorities

User_AYF65
User_AYF65 Member Posts: 135 Red Ribbon
edited September 2017 in New To Java

Hi,

I have a java program which creates 3 threads. I am trying to print thread names by associating a count (i.e from 1-100) in order to differentiate them. Threads are assigned priorities but when i run the program, i find that there is no impact of priorities and instead threads are executing in a random fashion and i am getting a jumbled output. I want the thread1 to run to completion first, then thread2 and so on according to the concept of priority based scheduling. But this is not happening. I dont know why? Some body guide me?

public class ThreadsSyncPriority extends Thread{

private String name;

ThreadsSyncPriority(String s) {

   name=s;

}

public void run() {

   for(int i=0;i<100;++i)

System.out.print(name+" " + i + "\n");

    

}

public static void main(String args[]){

 

 

 

   ThreadsSyncPriority obj1 = new ThreadsSyncPriority("first");

obj1.setPriority(Thread.MAX_PRIORITY);

   obj1.start();

   ThreadsSyncPriority obj2 = new ThreadsSyncPriority("Second");

obj2.setPriority(Thread.NORM_PRIORITY);

   obj2.start();

   ThreadsSyncPriority obj3 = new ThreadsSyncPriority("Third");

obj3.setPriority(Thread.MIN_PRIORITY);

   obj3.start();

}

}

first 0

Second 1

Third 1

Second 2

first 1

Second 3

Third 2

Second 4

first 2

Second 5

Third 3

Second 6

first 3

Second 7

Third 4

Second 8

first 4

Second 9

Third 5

Second 10

first 5

Second 11

Third 6

Second 12

first 6

Second 13

Third 7

Second 14

first 7

Second 15

Third 8

Second 16

first 8

Second 17

Third 9

Second 18

first 9

Second 19

Third 10

Second 20

first 10

Second 21

Third 11

Second 22

first 11

Second 23

Third 12

Second 24

first 12

Second 25

Third 13

Second 26

first 13

Second 27

Third 14

Second 28

first 14

Second 29

Third 15

Second 30

first 15

Second 31

Third 16

Second 32

first 16

Second 33

Third 17

Second 34

first 17

Second 35

Third 18

Second 36

first 18

Second 37

Third 19

Second 38

first 19

Second 39

Third 20

Second 40

first 20

Second 41

Third 21

Second 42

first 21

Second 43

Third 22

Second 44

first 22

Second 45

Third 23

Second 46

first 23

Second 47

Third 24

Second 48

first 24

Second 49

Third 25

Second 50

first 25

Second 51

Third 26

Second 52

first 26

Second 53

Third 27

Second 54

first 27

Second 55

Third 28

Second 56

first 28

Second 57

Third 29

Second 58

first 29

Second 59

Third 30

Second 60

first 30

Second 61

Third 31

Second 62

first 31

Second 63

Third 32

Second 64

first 32

Second 65

Third 33

Second 66

first 33

Second 67

Third 34

Second 68

first 34

Second 69

Third 35

Second 70

Second 71

first 35

Second 72

Third 36

Second 73

first 36

Second 74

Third 37

Second 75

first 37

Second 76

Third 38

Second 77

first 38

Second 78

Third 39

Second 79

first 39

Second 80

Third 40

Second 81

first 40

Second 82

Third 41

Second 83

first 41

Second 84

Third 42

Second 85

first 42

Second 86

Third 43

Second 87

first 43

Second 88

Third 44

Second 89

first 44

Second 90

Third 45

Second 91

first 45

Second 92

Third 46

Second 93

first 46

Second 94

Third 47

Second 95

first 47

Second 96

Third 48

Second 97

first 48

Second 98

Third 49

Second 99

first 49

Third 50

first 50

Third 51

first 51

Third 52

first 52

Third 53

first 53

Third 54

first 54

Third 55

first 55

Third 56

first 56

Third 57

first 57

Third 58

first 58

Third 59

first 59

Third 60

first 60

Third 61

first 61

Third 62

first 62

Third 63

first 63

Third 64

first 64

Third 65

first 65

Third 66

first 66

Third 67

first 67

Third 68

first 68

Third 69

first 69

Third 70

first 70

Third 71

first 71

Third 72

first 72

Third 73

first 73

Third 74

first 74

Third 75

first 75

Third 76

first 76

Third 77

first 77

Third 78

first 78

Third 79

first 79

Third 80

first 80

Third 81

first 81

Third 82

first 82

Third 83

first 83

Third 84

first 84

Third 85

first 85

Third 86

first 86

Third 87

first 87

Third 88

first 88

Third 89

first 89

Third 90

first 90

Third 91

first 91

Third 92

first 92

Third 93

first 93

Third 94

first 94

Third 95

first 95

Third 96

first 96

Third 97

first 97

Third 98

first 98

Third 99

first 99

Some body please guide me.

Zulfi.

Answers

  • Unknown
    edited September 2017

    Please edit your post and remove all but a few of the output lines.

    I have a java program which creates 3 threads. I am trying to print thread  names from 1-100 by assigning priorities but i am getting jumbled output. I want the thread1 to run first to completion , then thread thread2 and so on. My code is: 

    If you don't want the other threads to 'sleep' or 'wait' then you have to add code to make them do that.

    Threads are independent and have no idea what, if anything, other threads are doing unless you add variables/code to synchronize them.

    I suggest you learn how to use threads/functionality BEFORE trying to use them. The Java Tutorials has MANY trails about threads and concurrency.

    https://docs.oracle.com/javase/tutorial/essential/concurrency/sync.html

    Synchronization

    Threads communicate primarily by sharing access to fields and the objects reference fields refer to. This form of communication is extremely efficient, but makes two kinds of errors possible: thread interference and memory consistency errors. The tool needed to prevent these errors is synchronization.
  • User_AYF65
    User_AYF65 Member Posts: 135 Red Ribbon
    edited September 2017

    Hi,

    I found the synchronized block from the link you provided:

    synchronized (this){

    :

    :

    }

    I applied it to my code but i am getting the same problem. My code is:

    class Threads2SyncSolve extends Thread{

    private String name;

    Threads2SyncSolve(String s) {

       name=s;

    }

    public void run() {

    synchronized(this){

       for(int i=0;i<100;++i)

          System.out.print(name+" " + i + "\n");

    }

         

    }

    public static void main(String args[]){

       Threads2SyncSolve obj1 = new Threads2SyncSolve("first");

       Threads2SyncSolve obj2 = new Threads2SyncSolve("Second");

       Threads2SyncSolve obj3 = new Threads2SyncSolve("Third");

       obj1.start();

       obj2.start();

       obj3.start();

    }

    }

    Please guide me how to fix this problem. I dont find any code in the Atomic section of the java doc.

    Zulfi.

  • Unknown
    edited September 2017
    I found the synchronized block from the link you provided:

    Wrong section - that has NOTHING to do with synchronizing threads.

    You need to review the entire chain of trails for 'Threads'.

    In particular see the section on 'joins'.

    The join method allows one thread to wait for the completion of another. If t is a Thread object whose thread is currently executing,t.join(); 

    causes the current thread to pause execution until t's thread terminates. Overloads of join allow the programmer to specify a waiting period. However, as with sleep, join is dependent on the OS for timing, so you should not assume that join will wait exactly as long as you specify.

    Like sleep, join responds to an interrupt by exiting with an InterruptedException.

    You are the ONLY one that knows ALL of your requirements. You haven't provided ANY info that tells us what you are really trying to do. The example you gave with numbers is just plain silly since it wouldn't be of any real use.

    The simplest way to keep the other threads from executing out of order is to just not start the second one until the first one finishes.

This discussion has been closed.