Current code in MyThread class
public class MyThread extends Thread{
public static volatile AtomicInteger priority = new AtomicInteger(1);
public synchronized void correctPriority(){
int newPriority = priority.getAndIncrement();
newPriority = getThreadGroup() != null && newPriority > getThreadGroup().getMaxPriority() ? getThreadGroup().getMaxPriority() : newPriority;
setPriority(newPriority);
if(priority.intValue() > MAX_PRIORITY){
priority.set(1);
}
}
public MyThread() {
super();
correctPriority();
}
Code in main that prints the thread numbers
public class Solution {
public static void main(String[] args) {
for (int i = 0; i < 12; i++) {
System.out.print(new MyThread().getPriority() + " ");
}
// Output
// 1 2 3 4 5 6 7 8 9 10 1 2
System.out.println();
ThreadGroup group = new ThreadGroup("someName");
group.setMaxPriority(7);
for (int i = 0; i < 12; i++) {
System.out.print(new MyThread(group, "name" + i).getPriority() + " ");
}
// Output
// 3 4 5 6 7 7 7 7 1 2 3 4
}
}