Commits

Nzen  committed f828d21

Sync toy looks better, moved to package

Moved them to a package since they are for our reference rather than
relevant to the project. I may try out a concurrentSkipListMap later.
Committing at this point because this algorithm is too simple to simulate
the dhcp operations, but is still clear enough to identify what to
replace.

In the next phase, WithSyncOnly threads will search a shared array of
counters for those at zero and then sleep/decrement them to zero. Then the
transition to Dhcp only involves adding more fields to the internal class,
writing notifications, & listening for early releases.

  • Participants
  • Parent commits b8cc55f

Comments (0)

Files changed (3)

File sharedDS/DataCorruptor.java

+package sharedDS;
+
+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( );
+	}
+
+	/*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 )
+	{
+		System.out.print( "shoved\n" );
+	}
+	nn.printSump();
+	*/
+}

File sharedDS/ThreadWithSyncList.java

+package sharedDS;
+import java.util.List;
+import java.util.ArrayList;
+import java.util.Collections;
+
+public class ThreadWithSyncList implements Runnable
+{
+	final int SIZE = 5;
+	protected static List< Boolean > sump = startSump();
+	String threadName;
+	Thread myself;
+	
+	// static initializer
+	public static List<Boolean> startSump()
+	{
+		// fill with garbage so set( )s don't throw "out_of_bounds"
+		ArrayList<Boolean> temp = new ArrayList<Boolean>( 6 ); // literal since I didn't want a static SIZE
+		for ( int nn = 6; nn > 0; nn-- )
+			temp.add( false );
+		temp.set( 0, true );
+		return Collections.synchronizedList( temp );
+		// I've been warned that, hereafter, if temp is modified rather than sump,
+		// it will defy synchronization, which is a danger to an instance based communal List.
+	}
+	
+	public ThreadWithSyncList()
+	{
+		myself = new Thread( this );
+		myself.start();
+	}
+	
+	public ThreadWithSyncList( String name )
+	{
+		myself = new Thread( this );
+		myself.start();
+		this.threadName = name;
+	}
+	
+	public void run()
+	{
+		int recover;
+		try
+		{
+			recover = wreakHavoc();
+			Thread.sleep( recover * 500 ); // to have them access at different times
+			wreakHavoc();
+			wreakHavoc();
+			//removeHavoc();
+			System.out.println( threadName + "-fin" );
+		}
+		catch ( InterruptedException wups )
+		{ 
+			//Thread.yield( ); // back of the thread queue for you
+			System.out.print( "shoved\n" );
+		}
+	}
+	
+	// just a method to iterate and mutate values analagous to dhcp
+	// should set correctly since I'm using the synchronized list
+	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;
+		Boolean flipped;
+		synchronized( ThreadWithSyncList.sump ) // synchList demands manual sync for iteration
+		{
+			while ( ind <= SIZE )
+			{
+				flipped = ThreadWithSyncList.sump.get( ind ); // tedious: hazard of static field
+				if ( flipped )
+				{
+					if ( ind == SIZE )
+						// all are true
+						ThreadWithSyncList.sump.set( 0, ! flipped );
+					else // induct another
+						ThreadWithSyncList.sump.set( ind + 1, flipped );
+					break;
+				}
+				ind++;
+			}
+		}
+		// result: X < 9 values are true, beginning from back
+		//	except if head is true, then all true except tail.
+		//printSump();
+		return ( ind == SIZE ) ? SIZE : ind + 1;
+	}
+	
+	synchronized void printSump()
+	{
+		for ( Boolean xY : ThreadWithSyncList.sump )
+			System.out.print( threadName + ( ( xY ) ? "y " : "N " ) );
+		System.out.println( );
+	}
+}
+
+/* for better simulation later, tell me which thread changed it
+class ynId
+{
+	boolean yes;
+	int id;
+	
+	public ynId( )
+	{
+		yes = true;
+		id = -1;
+	}
+	
+	
+}
+*/
+
+/*
+-- Discarded, don't want to initialize five iterators.
+Iterator<Boolean> syncSafeNow = ThreadWithArrList.sump.iterator();
+// flip a true
+while ( syncSafeNow.hasNext() )
+{
+	flipped = syncSafeNow.next();
+	if ( flipped )
+	{
+		ThreadWithArrList.sump.set( ind, ! flipped );
+		break;
+	}
+	ind++;
+}
+*/

File sharedDS/WithSyncOnly.java

+package sharedDS;
+
+import java.lang.StringBuilder;
+
+public class WithSyncOnly implements Runnable
+{
+	String threadName;
+	Thread myself;
+	boolean[] bits;
+	
+	public WithSyncOnly( String name, boolean[] yN )
+	{
+		this.threadName = name;
+		bits = yN;
+		myself = new Thread( this );
+		myself.start();
+	}
+
+	public void run()
+	{
+		int recover;
+		try
+		{
+			recover = wreakHavoc();
+			print();
+			Thread.sleep( 10000 - ( 100 * recover ) ); // to have them access at different times
+			wreakHavoc();
+			//removeHavoc();
+			print();
+		}
+		catch ( InterruptedException wups )
+		{ 
+			Thread.yield( ); // back of the thread queue for you
+		}
+	}
+	
+	// just a method to iterate and mutate values analagous to dhcp
+	// should set correctly since I'm using the synchronized list
+	int wreakHavoc()
+	{
+		/*
+		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
+		{
+			while ( ind <= lim )
+			{
+				flipped = bits[ ind ];
+				if ( flipped )
+				{
+					if ( ind == 0 ) // all are true
+						bits[ lim ] = ! flipped; // in case I want to recoverHavoc();
+					else // induct previous
+						bits[ ind - 1 ] = flipped;
+					break;
+				}
+				ind++;
+			}
+		}
+		// result: some values are true, beginning from back
+		//	except if head is true, then all true except tail.
+		return ind + 1;
+	}
+	
+	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 )
+		{
+			for ( boolean xY : bits )
+				viewOfBits.append( ( xY ) ? "y " : "N " );
+		}
+		viewOfBits.append( "\n" );
+		System.out.print(  viewOfBits );
+	}
+}
+
+
+
+
+
+
+
+
+
+
+
+
+