Roshan N P
BAN USER
Natural number series generated using N Thread
public class SeriesGeneratorDemo {
public static final int NUMBER_OF_THREAD = 5;
public static void main(String[] args) {
// TODO code application logic here
SequenceGenerator sequenceGenerator=new SequenceGenerator();
for (int i = 1; i <= NUMBER_OF_THREAD; i++) {
new Thread(sequenceGenerator,i+"").start();
}
}
}
class SequenceGenerator implements Runnable {
private static final int SERIES_MAX_NUMBER = 100;
private static int nextThreadName=1;
private static int nextNumber = 0;
@Override
public void run() {
while (nextNumber < SERIES_MAX_NUMBER) {
synchronized (this) {
int currentThread = Integer.parseInt(Thread.currentThread().getName());
if (currentThread==nextThreadName) {
generateNumber();
if (currentThread < SeriesGeneratorDemo.NUMBER_OF_THREAD) {
nextThreadName = currentThread + 1;
} else {
nextThreadName = 1;
}
this.notifyAll();
} else {
try {
this.wait();
} catch (InterruptedException ex) {
System.out.println(ex);
}
}
}
}
}
public synchronized void generateNumber() {
System.out.println("Thread Name : "+Thread.currentThread().getName()+" value : "+ (++nextNumber));
}
}
Repmendezleah216, Analyst at Bosch
Hello,everybody,I'm Leah Mendez.I want to make some friends here.I’m a woman with ambition and ...
Repjoyceeallard, Associate at Adap.tv
Hi, i am working as a training manager as a business professional who assesses the growth and development needs of ...
Repmarkglove17, Associate at Arista Networks
Hi, I am Mark from North Edwards, CA .I am just crazy about dance.I am not professionally trained, but ...
Repmakaylamelua, Blockchain Developer at AMD
I am a sound editing and music composer with experience of handling a wide variety of programs.During my free ...
RepI'm from India, 26 years old. I want to travel, I want a job where I can earn money ...
Reprchierusel, Applications Developer at Allegient
I am a Real-time captioner in the USA . Real-time captioning can be used for programs that do not have written ...
Repjanicepdaniels1, Backend Developer at Accenture
I decided to become an entrepreneur and work for myself because I wasn't making the money I wanted to ...
Repcarmelalewis9, Area Sales Manager at AMD
I am fond of reading stories then reading articles which are all about a story of a beautiful nature and ...
Repkylecfarrell, Personnel at Bocada
Property custodian with a reputed organization and help in keeping accurate check over the supplies and inventory of materials and ...
Repannehbell8, Quality Assurance Engineer at Globaltech Research
Build and maintain relationships with convention vendors. Resolve issues throughout meeting event timelines.Plan room layouts and event programs, schedule ...
public class PrintNumbers {
- Roshan N P June 10, 2014static int n = 13;
public static void main(String[] args) {
for (int i = 1; i < n; i++) {
int k = n / i;
StringBuilder sb = new StringBuilder("");
for (int j = 1; j <= k; j++) {
if (j == 1) {
sb.append(i);
} else {
sb.append(" + " + i);
}
}
if (i * k < n) {
display(n - (i * k), sb.toString());
} else {
System.out.println(sb.toString());
}
}
}
public static void display(int n, String str) {
for (int i = 1; i <= n; i++) {
StringBuilder sb = new StringBuilder(str);
int k = n / i;
for (int j = 1; j <= k; j++) {
sb.append(" + " + i);
}
if (i * k < n) {
display(n - (i * k), sb.toString());
} else if (i * k == n) {
System.out.println(sb.toString());
}
}
}
}
Output number T=13
1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1
2 + 2 + 2 + 2 + 2 + 2 + 1
3 + 3 + 3 + 3 + 1
4 + 4 + 4 + 1
5 + 5 + 1 + 1 + 1
5 + 5 + 2 + 1
5 + 5 + 3
6 + 6 + 1
7 + 1 + 1 + 1 + 1 + 1 + 1
7 + 2 + 2 + 2
7 + 3 + 3
7 + 4 + 1 + 1
7 + 4 + 2
7 + 5 + 1
7 + 6
8 + 1 + 1 + 1 + 1 + 1
8 + 2 + 2 + 1
8 + 3 + 1 + 1
8 + 3 + 2
8 + 4 + 1
8 + 5
9 + 1 + 1 + 1 + 1
9 + 2 + 2
9 + 3 + 1
9 + 4
10 + 1 + 1 + 1
10 + 2 + 1
10 + 3
11 + 1 + 1
11 + 2
12 + 1