Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class ContiniousPrintDemo {
- public static void main(String[] args) {
- Counter count = new Counter();
- Thread t = new Thread(new Runnable(){
- @Override
- public void run() {
- synchronized (count) {
- for(int i=0;i<=10;i=i+2){
- if(!count.isEven()){
- try {
- count.wait();
- } catch (InterruptedException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- System.out.println(i);
- count.setEven(false);
- count.notify();
- }
- }
- }
- });
- Thread t1 = new Thread(new Runnable(){
- @Override
- public void run() {
- synchronized (count) {
- for(int i=1;i<10;i=i+2){
- if(count.isEven){
- try {
- count.wait();
- } catch (InterruptedException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- System.out.println(i);
- count.setEven(true);
- count.notify();
- }
- }
- }
- });
- t.start();
- t1.start();
- }
- }
- class Counter{
- boolean isEven =true;
- public boolean isEven() {
- return isEven;
- }
- public void setEven(boolean isEven) {
- this.isEven = isEven;
- }
- }
Add Comment
Please, Sign In to add comment