Skip to content

Instantly share code, notes, and snippets.

@kineticz
Last active August 29, 2015 14:14
Show Gist options
  • Select an option

  • Save kineticz/bbbd2f307714f3203b54 to your computer and use it in GitHub Desktop.

Select an option

Save kineticz/bbbd2f307714f3203b54 to your computer and use it in GitHub Desktop.
why aren't the x's printed when PrintNum reaches 50?
package je3.thread;
/**
* Created by IDEA on 01/02/15.
*/
public class PrintChar implements Runnable {
private char charToPrint;
private int times;
public PrintChar(char c, int t) {
charToPrint = c;
times = t;
}
public PrintChar(char b) {
this(b, 100);
}
@Override
public void run() {
for(int i = 0; i < times; i++) {
System.out.print(charToPrint);
}
}
}
package je3.thread;
/**
* Created by IDEA on 01/02/15.
*/
public class PrintNum implements Runnable {
private int lastNum;
public PrintNum(int n) {
lastNum = n;
}
@Override
public void run() {
Thread thread4 = new Thread(
new PrintChar('x', 40)
);
try {
for (int i = 1; i <= lastNum; i++) {
System.out.print(i + " ");
if(i == 50) {
thread4.join();
}
}
}
catch (InterruptedException e) {}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment