# New to Java

## Thread Synchronization using Priorities

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.