0

Yes, this is a homework assignment, but I have tried everything possible and can't come up with a possible. The point of this assignment is to illustrate that, before implementing the dekker's algorithm / peterson's algorithm, it is very likely that two processes will not go one after another.

 import java.util.*;

public class myProcess
{

     private static final Random R = new Random();
     private int id;


     public myProcess(int i){
         id = i;
    }


    private static void delay(int value){

         try{
             java.lang.Thread.sleep(R.nextInt(value));
         }
         catch(InterruptedException e){
         }
     }

     public void run(){              

         System.out.println("");
         delay(20);
         System.out.println(this.id + " is starting");
         delay(20);
         System.out.println("LINE ONE");
         delay(20);
         System.out.println("LINE TWO");
         delay(20);
         System.out.println("LINE THREE");
         delay(20);
         System.out.println(this.id+ " is ending ");
         delay(20);

     }

     public static void main(String [] args){
         final int N = 2;
         myProcess[] t = new myProcess[N];

         for(int i = 0; i < N; i++){
             t[i] = new myProcess(i);
             t[i].run();

         }


  }

Right now the output is

 0 is starting
 LINE ONE
 LINE TWO
 LINE THREE
 0 is ending
 1 is starting
 LINE ONE
 LINE TWO
 LINE THREE
 1 is ending

but it should be all mixed up to illustrate that processes don't necessarily wait for another one to finish.

I tried other methods of defining run() such as

 String[] statements = new String[5];
         statements[0] = "Thread " + this.id + " is starting iteration ";
         statements[1] = "We hold these truths to be self-evident, that all men are          created equal,";
         statements[2] = "that they are endowed by their Creator with certain unalienable Rights,";
         statements[3] = "that among these are Life, Liberty and the pursuit of Happiness.";
         statements[4] = "Thread " + this.id+ " is done with iteration ";

    for(int i = 0; i< 5; i++){

        System.out.println(statements[i]);
        delay(20);
    } 

but it still does not return to me any "wrong outputs"

What am I doing so wrong that's making the output so right?

user3277633
  • 1,891
  • 6
  • 28
  • 48

3 Answers3

2

You should call start() function on your thread, not run().

Edit: Also your class should implement Runnable interface or extend Thread class.

You are not creating new Threads in your code and everything is running in one thread.

public class myProcess extends Thread

...

for(int i = 0; i < N; i++){
    t[i] = new myProcess(i);
    t[i].start();
}
Robert Balent
  • 1,452
  • 11
  • 21
  • can you elaborate more on the start() function as oppose to the run() ? is start() a pre-defined class in Thread? My instructor said something along the same thing but I assumed that since start() simply initializes run(), I can just call run() and achieve the same effect. Or is that logic wrong? edit: if i do have to implement a start() functino, how is that different from the run() I already defined? – user3277633 Mar 31 '14 at 20:22
  • When you call `run()` function, you are calling it in the same thread. On the other side `start()` function will create new thread and execute `run()` function in the background. Walk quickly through tutorial and everything will be clear: http://docs.oracle.com/javase/tutorial/essential/concurrency/ – Robert Balent Apr 01 '14 at 08:48
0

I would guess that your delays are too short to see any significant mixing. You pass in 20, as if it's 20 seconds, but it's only 20 milliseconds of sleep. Pass in 20,000 and see if you get the behavior you expect.

Zagrev
  • 2,000
  • 11
  • 8
  • My instructor told us to randomly use a time between 1 - 20 msec, but I'll try it with 20 seconds and see how it goes! Edit: so I tried it with 20, 200, 2000, and 20,000 and it still does not work. – user3277633 Mar 31 '14 at 03:38
0

Change your delay method to look like the following. According to this post (https://stackoverflow.com/a/1600603/1265692), the Java sleep method is not guaranteed to relinquish control over the cpu. By adding the yield call, you remind Java to let other Threads run.

private static void delay(int value){

     try{
         java.lang.Thread.sleep(R.nextInt(value));
         java.lang.Thread.yield();
     }
     catch(InterruptedException e){
     }
 }
Community
  • 1
  • 1
ashwin153
  • 364
  • 2
  • 13