Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Thread Synchronization using Priorities

Zulfi KhanSep 5 2017 — edited Sep 7 2017

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.

Comments

807589
It's a problem with all proportional-spaced fonts, including western fonts -- the "M" is much wider than the "I", especially in a san-serif font.

As a result, you always have to set column widths for an estimated number of characters. The method [Font.getMaxCharBounds()|http://java.sun.com/j2se/1.5.0/docs/api/java/awt/Font.html#getMaxCharBounds(java.awt.font.FontRenderContext)] will give you the maximum space that any character in the font will occupy, and you an either base you column size on that or some fraction of that.
JoachimSauer
Edit: Sorry, I misread your question and answered a slightly different one. Feel free to ignore the following text ;-)

I assume you're talking about Swing.

First solution: Don't handle it yourself, simply use a Label that draws it as it wishes.
If you want to draw it yourself, use drawString(), which handles that for you.
If you still need to know more detail, try Font.createGlyphVector(), which will tell you exactly how Java will position the characters.
807589
Hi,
Thanks a lot for answering, but I am not using Swings. I am trying to display some Japanese characters, which is actually causing the problem
807589
Thanks a lot for answering, but I am not using Swings. I am trying to display some Japanese characters, which is actually causing the problem
And what, pray tell, are you using to display those characters?
807589
Hi,
thanks a lot for answering. My problem is specific to some unicode Characters. I havent faced any issue with ASCII so far. Can Font.getMaxCharBounds() be using for different charsets too?
807589
I am trying to display them in Windows Command line
JoachimSauer
Vannan wrote:
thanks a lot for answering. My problem is specific to some unicode Characters. I havent faced any issue with ASCII so far. Can Font.getMaxCharBounds() be using for different charsets too?
Java is purely unicode and doesn't differentiate between charsets at that level. A Java String object is always represented in UTF-16 internally and can be handled as if it where pure Unicode bliss.

So this question doesn't make any sense.
I am trying to display them in Windows Command line
Forget it. The Windows console can display some ISO-8859 variations at most and most definitely can't correctly handle those characters. Choose another output format.
807589
I am trying to display them in Windows Command line
Java has absolutely no control over how the Windows displays characters on the command line, nor any ability to discover the fonts used to do so.

Unless your particular Windows implementation uses a fixed-width font that supports all characters needed, you are as JoachimSauer said, out of luck. If it does support such a character set, you will need to ensure that the default encoding for your Java program is the same as you Windows installation.
807589
@JoachimSauer

"Java is purely unicode and doesn't differentiate between charsets at that level. A Java String object is always represented in UTF-16 internally and can be handled as if it where pure Unicode bliss."

My problem occurs when I try to display those characters on a Terminal (it can be a linux Terminal also).
I have to display the Jap characters with a proper alignment, say in a space of 30 columns and truncate if any.
The problem is because each of the characters might occupy different space positions in the terminal. So i cant predetermine the number of characters to truncate or number of white spaces to append (so that it gets aligned in a column of say 30 spaces) unless there is a way to do so in JAVA. Initially i thought number of bytes in the character will be proportional to the space position occupied but its not the case.
My output for eg shud be:

Col A------ ColB------ ColC------
abcdefgh asdasdwa asdasdas

Am I clear?
JoachimSauer
There is no "number of bytes" in a pure Java String (no, getBytes() only returns the bytes in your platforms default encoding, it has nothing to do with how the String is stored in memory).

Also, as you noticed the length of the String in bytes by some specified encoding doesn't necessarily correlate with its visible width.

If you need such advanced layouting then you should really choose a different output medium instead of the console. I'd suggest HTML output for example (in which case the Browser will handle all of the complex width-measurement).

Also, I'd be very surprised if the Windows console can display those double-width characters at all (some (most modern) Linux terminals can display all Unicode characters for which they can find a Font).

If you absolutely want to do it on the console, then find a copy of the unicode standard and find out which characters in there are defined to be Double-Width characters and calculate it yourself (or be very pessimistic and assume every character is double-width).
807589
Thanks a lot JoachimSauer for your reply.
Yeah I was to go by the pessimistic approach, if I am absolutely sure that there is no way out.
1 - 11
Locked Post
New comments cannot be posted to this locked post.

Post Details

Locked on Oct 5 2017
Added on Sep 5 2017
3 comments
435 views