Commits

Nzen  committed 5b04011

Representative counter action

WithSyncOnly displays intended behavior: threads find an untimed counter
in a shared array, decrement from it, and search for another one. The
run pasted in DataCorruptor shows that the threads can take different
orders if looking before the releaser starts looking.

As noted, fulfilling our DHCP spec utilizes a similar sequence. An
analagous class would notify client when it choseCounter() and instead of
decrementing times (which is to simulate different client threads). If the
thread listened after sleeping and received a cancellation, it could break
from monitoring.

  • Participants
  • Parent commits f828d21

Comments (0)

Files changed (3)

File DataCorruptor.java

-
-public class DataCorruptor
-{
-
-	public static void main(String[] args)
-	{
-		/*Thread[] barrel = new Thread[ 2 ];
-		for ( Thread monkey : barrel )
-		{
-			monkey = new Thread( new ThreadWithArrList( ) );
-			//monkey.start(); // constructor starts the thread.
-		}*/
-		ThreadWithArrList nn = new ThreadWithArrList( "1" );
-		ThreadWithArrList cc = new ThreadWithArrList( "2" );
-		ThreadWithArrList uu = new ThreadWithArrList( "3" );
-	}
-
-}

File sharedDS/DataCorruptor.java

 
 public class DataCorruptor
 {
-	static boolean[] nnUU = { false, false, false, false, false, false, false, false, false, true }; // 10
 
 	public static void main(String[] args)
 	{
-		// init shared bool array
-		boolean[] yN = { false, false, false, false, false, false, false, false, false, true }; // 10
-		WithSyncOnly nn = new WithSyncOnly( "1", yN );
-		WithSyncOnly uu = new WithSyncOnly( "2", yN );
-		//nn.print();
-		//wreakHavoc();
-		//print();
-	}
-	
-	static int wreakHavoc()
-	{
-		/* find first with a true
-		// if this is tail
-			// set head to false
-		// else
-			// set next to true
-		// print list */
-		int ind = 0;
-		int lim = DataCorruptor.nnUU.length - 1;
-		boolean flipped;
-		while ( ind <= lim )
-		{
-			flipped = DataCorruptor.nnUU[ ind ];
-			if ( flipped )
-			{
-				if ( ind == 0 ) // all are true
-					DataCorruptor.nnUU[ lim ] = ! flipped; // make last false
-				else // induct previous
-					DataCorruptor.nnUU[ ind - 1 ] = flipped;
-				break;
-			}
-			ind++;
-		}
-		return ind + 1;
-	}
-	
-	static void print( )
-	{
-		for ( boolean xY : DataCorruptor.nnUU )
-		System.out.print( ( xY ) ? "y " : "N " );
-		System.out.println( );
+		// init shared array
+		Counters[ ] clientIPs = { new Counters(), new Counters(), new Counters(), new Counters() };
+		WithSyncOnly nn = new WithSyncOnly( "1", clientIPs );
+		WithSyncOnly uu = new WithSyncOnly( "2", clientIPs );
+		WithSyncOnly cc = new WithSyncOnly( "3", clientIPs );
 	}
+}
 
-	/*Thread[] barrel = new Thread[ 2 ];
-	for ( Thread monkey : barrel )
-	{
-		monkey = new Thread( new ThreadWithArrList( ) );
-		//monkey.start(); // constructor starts the thread.
-	}
-	ThreadWithSyncList nn = new ThreadWithSyncList( "1" );
-	ThreadWithSyncList cc = new ThreadWithSyncList( "2" );
-	ThreadWithSyncList uu = new ThreadWithSyncList( "3" );
-	try
-	{
-		Thread.sleep(3000);
-	}
-	catch ( InterruptedException wups )
+class Counters // educational only so no get/set
+{
+	int left;
+	public Counters( )
 	{
-		System.out.print( "shoved\n" );
+		left = 0;
 	}
-	nn.printSump();
-	*/
 }
+/* Awesome run: threads switched which index to monitor
+1.0	3 0 0 0 
+3.2	3 3 3 0 
+2.1	3 3 0 0 
+1.0	2 2 2 0 
+2.1	2 2 2 0 
+3.2	2 2 2 0 
+1.0	1 1 1 0 
+3.2	1 1 1 0 
+2.1	1 1 1 0 
+3  next
+1  next
+3.0	3 0 0 0 
+2  next
+1.1	3 3 0 0 
+2.2	2 3 3 0 
+1.1	2 2 2 0 
+3.0	2 2 2 0 
+2.2	2 2 2 0 
+1.1	1 1 1 0 
+3.0	1 1 1 0 
+2.2	1 1 1 0 
+3  next
+1  next
+3.0	3 0 0 0 
+2  next
+1.1	3 3 0 0 
+2.2	2 3 3 0 
+1.1	2 2 2 0 
+2.2	2 2 2 0 
+3.0	2 2 2 0 
+1.1	1 1 1 0 
+2.2	1 1 1 0 
+3.0	1 1 1 0 
+2  next
+1  next
+3  next
+
+*/

File sharedDS/WithSyncOnly.java

 {
 	String threadName;
 	Thread myself;
-	boolean[] bits;
+	int myIndex;
+	Counters[ ] waits;
 	
-	public WithSyncOnly( String name, boolean[] yN )
+	public WithSyncOnly( String name,Counters[ ] all0 )
 	{
+		myIndex = -1;
 		this.threadName = name;
-		bits = yN;
+		waits = all0;
 		myself = new Thread( this );
 		myself.start();
 	}
 
 	public void run()
 	{
-		int recover;
+		int times = 3;
 		try
 		{
-			recover = wreakHavoc();
-			print();
-			Thread.sleep( 10000 - ( 100 * recover ) ); // to have them access at different times
-			wreakHavoc();
-			//removeHavoc();
-			print();
+			while ( times > 0 ) // fake optimization: counting down
+			{
+				if ( choseCounter() )
+				{
+					while ( monitoringCounter() ) // side eff: counter--
+						Thread.sleep( 500 );
+					times--;
+					System.out.print( threadName +"  next\n" );
+				}
+				else // all taken 
+				{
+					//System.out.print( "blocked from arr\n" );
+					Thread.sleep( 250 );
+					continue;
+				}
+			}
 		}
 		catch ( InterruptedException wups )
 		{ 
 		}
 	}
 	
-	// just a method to iterate and mutate values analagous to dhcp
-	// should set correctly since I'm using the synchronized list
-	int wreakHavoc()
+	boolean choseCounter()
 	{
-		/*
-		find lowest index with a true
-		if this is head
-			set tail to false
-		else
-			set previous to true
-		print list
-		*/
 		int ind = 0;
-		int lim = bits.length - 1;
-		boolean flipped;
-		synchronized( bits ) // should lock bits for the duration
+		int lim = waits.length - 1;
+		// find a counter to watch over
+		synchronized( waits )
 		{
 			while ( ind <= lim )
 			{
-				flipped = bits[ ind ];
-				if ( flipped )
+				if ( waits[ ind ].left < 1 )
 				{
-					if ( ind == 0 ) // all are true
-						bits[ lim ] = ! flipped; // in case I want to recoverHavoc();
-					else // induct previous
-						bits[ ind - 1 ] = flipped;
+					myIndex = ind;
 					break;
 				}
 				ind++;
 			}
+			// check that I got one
+			if ( myIndex < 0 )
+				return false;
+			else
+			{
+				// set wait time
+				waits[ myIndex ].left = 3;
+				return true;
+			}
+		}
+	}
+	
+	// decrement counter, return true if still above 0
+	synchronized boolean monitoringCounter()
+	{
+		print();
+		// decrement my counter
+		waits[ myIndex ].left--;
+		if ( waits[ myIndex ].left < 1 )
+		{ // vigil ends
+			myIndex = -1;
+			return false;
 		}
-		// result: some values are true, beginning from back
-		//	except if head is true, then all true except tail.
-		return ind + 1;
+		return true; // more time to live
 	}
 	
 	void print() // printing waits for I/O, so I save state with a string & print that
 	{
-		StringBuilder viewOfBits = new StringBuilder( 25 );
-		viewOfBits.append( threadName + "\t" );
-		synchronized( bits )
+		StringBuilder queue = new StringBuilder( 25 );
+		queue.append( threadName + '.' + myIndex + "\t" );
+		synchronized( waits )
 		{
-			for ( boolean xY : bits )
-				viewOfBits.append( ( xY ) ? "y " : "N " );
+			for ( Counters dur : waits )
+				queue.append( Integer.toString( dur.left ) + " " );
 		}
-		viewOfBits.append( "\n" );
-		System.out.print(  viewOfBits );
+		queue.append( "\n" );
+		System.out.print( queue );
 	}
 }