ww2.justanswer.com/uploads/ComputersGuru/2010-07-2...

burgerraraSoftware and s/w Development

Nov 18, 2013 (3 years and 7 months ago)

79 views

1)

import

java.lang.Runnable;


public

class

ArrayWriter2
implements

Runnable

{


private

final

SimpleArray
sharedSimpleArray
;


private

final

int

startValue
;



public

ArrayWriter2(
int

value, SimpleArray array )


{


startValue

= value;


sharedSimpleArray
= array;


}
// end constructor



public

void

run()


{


for

(
int

i =
startValue
; i <
startValue

+ 3; i++ )


{




char

charValue = ((
char
)i);
// modification to char


sharedSimpleArray
.add( charValue );
// add an
element to the
shared array


}
// end for


}
// end method run

}
// end class ArrayWriter



Questions:

1.

What does (char)i do?

2.

What is the difference between Thread class and Runnable interface?




/**************************************************************************


* (C) Copyright 1992
-
2007 by Deitel & Associates, Inc. and *


* Pearson Education, Inc. All Rights Reserved. *


* *


* DISCLAIMER: The authors and publisher of this book have used their *


* best efforts in preparing the book. These efforts include the *


* development, research, an
d testing of the theories and programs *


* to determine their effectiveness. The authors and publisher make *


* no warranty of any kind, expressed or implied, with regard to these *


* programs or to the documentation contained in these b
ooks. The authors *


* and publisher shall not be liable in any event for incidental or *


* consequential damages in connection with, or arising out of, the *


* furnishing, performance, or use of these programs. *


*******
******************************************************************/



import

java.util.concurrent.Executors;

import

java.util.concurrent.ExecutorService;

import

java.util.concurrent.TimeUnit;


public

class

SharedArrayTest2

{


public

static

void

main( String[] arg )


{


// construct the shared object


SimpleArray sharedSimpleArray =
new

SimpleArray( 6 );



// create two tasks to write to the shared SimpleArray


ArrayWriter2 writer1 =
new

ArrayWriter2( 97, sharedSimpleArray );
//
modified start value


ArrayWriter2 writer2 =
new

ArrayWriter2( 100, sharedSimpleArray );
//
modified start value



// execute the tasks with an ExecutorService


ExecutorService executor = Executors.
n
ewCachedThreadPool
();


executor.execute( writer1 );


executor.execute( writer2 );



executor.shutdown();



try


{


// wait 1 minute for both writers to finish executing


boolean

tasksEnded = executor.awaitTerminatio
n(


1, TimeUnit.
MINUTES

);



if

( tasksEnded )


System.
out
.println( sharedSimpleArray );
// print contents


else


System.
out
.println(


"Timed out while waiting for tasks to finish."

);


}
// end try


catch

( InterruptedException ex )


{


System.
out
.println(


"Interrupted while wait for tasks to finish."

);


}
// end catch


}
// end main

}
// end class SharedArrayTest


Questions:

1.

What is ExecutorService?



/**************************************************************************


* (C) Copyright 1992
-
2007 by Deitel & Associates, Inc. and *


* Pearson Education, Inc. All Rights Reserved. *


* *


* DISCLAIMER: The authors and publisher of this book have used their *


* best efforts in preparing the book. These efforts include the *


* development, research, an
d testing of the theories and programs *


* to determine their effectiveness. The authors and publisher make *


* no warranty of any kind, expressed or implied, with regard to these *


* programs or to the documentation contained in these b
ooks. The authors *


* and publisher shall not be liable in any event for incidental or *


* consequential damages in connection with, or arising out of, the *


* furnishing, performance, or use of these programs. *


*************************************************************************/


import

java.util.Random;


public

class

SimpleArray

{


private

final

char

array
[];
// the shared integer array modified to char
array


private

int

writeIndex

= 0;
// index of
next element to be written


private

final

static

Random
generator

=
new

Random();



// construct a SimpleArray of a given size


public

SimpleArray(
int

size )


{


array

=
new

char
[ size ];
//array modified to char


}
// end constructor



/
/ add a value to the shared array


public

synchronized

void

add(
char

value )
// int value changed to char


{


int

position =
writeIndex
;
// store the write index



try


{


// put thread to sleep for 0
-
499 milliseconds


Thread.
sleep
(
generator
.nextInt( 500 ) );


}
// end try


catch

( InterruptedException ex )


{


ex.printStackTrace();


}
// end catch



// put value in the appropriate element


array
[ position ] = value;


System.
out
.printf(
"%s wrote %s to element %d.
\
n"
,


Thread.
currentThread
().getName(), value, position );



++
writeIndex
;
// increment index of element to be written next


System.
out
.printf(
"Next write index: %d
\
n"
,
writeIndex

);


}
//
end method add



// used for outputting the contents of the shared integer array


public

String toString()


{


String arrayString =
"
\
nContents of SimpleArray:
\
n"
;



for

(
int

i = 0; i <
array
.
length
; i++ )


arrayString +=
array
[ i ] +
" "
;



return

arrayString;


}
// end method toString

}
// end class SimpleArray



Questions:

1.

What does
printStackTrace() do
?

2.

How to set the name of a thread?



/**************************************************************************


* (C) Copyright 1992
-
2007 by Deitel & Associates, Inc. and *


* Pearson Education, Inc. All Rights Reserved. *


* *


* DISCLAIMER: The authors and publisher of this book have used their *


* best efforts in preparing the book. These efforts include the *


* development, research, and testing of the theories and programs *


* to determine their effect
iveness. The authors and publisher make *


* no warranty of any kind, expressed or implied, with regard to these *


* programs or to the documentation contained in these books. The authors *


* and publisher shall not be liable in any event for in
cidental or *


* consequential damages in connection with, or arising out of, the *


* furnishing, performance, or use of these programs. *


*************************************************************************/



2)

import

java.lang.Runnable;


public

class

ArrayWriter
implements

Runnable

{


private

final

SimpleArray
sharedSimpleArray
;


private

final

char

startValue
;



public

ArrayWriter(
char

value, SimpleArray array )


{


startValue

= value;


sharedSimpleArray
= array;


}
// end constructor



public

void

run()


{


for

(
int

i = (
int
)
startValue
; i < (
int
)
startValue

+ 3; i++ )


{


sharedSimpleArray
.add( (
char
)i );
// add an element to the shared
array


}
// end for


}
// end method run

}
// end class ArrayWriter




Questions:

1.

What does
(
char
)i do
?

2.

What is the ASCII value for character ‘A’?



/**************************************************************************


* (C) Copyright 1992
-
2010 by Deitel &
Associates, Inc. and *


* Pearson Education, Inc. All Rights Reserved. *


* *


* DISCLAIMER: The authors and publisher of this book have used the
ir *


* best efforts in preparing the book. These efforts include the *


* development, research, and testing of the theories and programs *


* to determine their effectiveness. The authors and publisher make *


* no warranty of a
ny kind, expressed or implied, with regard to these *


* programs or to the documentation contained in these books. The authors *


* and publisher shall not be liable in any event for incidental or *


* consequential damages in connection with, or

arising out of, the *


* furnishing, performance, or use of these programs. *


*************************************************************************/




import

java.util.concurrent.Executors;

import

java.util.concurrent.ExecutorService;

import

java.util.concurrent.TimeUnit;


public

class

SharedArrayTest

{


public

static

void

main( String[] arg )


{


// construct the shared object


SimpleArray sharedSimpleArray =
new

SimpleArray( 6 );



// create two tasks to write to the shared SimpleArray


ArrayWriter writer1 =
new

ArrayWriter(
'a'
, sharedSimpleArray );


ArrayWriter writer2 =
new

ArrayWriter( (
char
)(
'a'

+ 11),
sharedSimpleArray );



// execute the tasks with an Exec
utorService


ExecutorService executor = Executors.
newCachedThreadPool
();


executor.execute( writer1 );


executor.execute( writer2 );



executor.shutdown();



try


{


// wait 1 minute for both writers to finish
executing


boolean

tasksEnded = executor.awaitTermination(


1, TimeUnit.
MINUTES

);



if

( tasksEnded )


System.
out
.println( sharedSimpleArray );
// print contents


else


System.
out
.println(


"Timed out while waiting for tasks to finish."

);


}
// end try


catch

( InterruptedException ex )


{


System.
out
.println(


"Interrupted while wait for tasks to finish."

);


}
// end catch


}
// end
main

}
// end class SharedArrayTest


Questions:

1.

What is ExecutorService?

2.

What is meaning of
(
char
)(
'a'

+ 11)
?



/**************************************************************************


* (C) Copyright 1992
-
2010 by Deitel & Associates, Inc. and

*


* Pearson Education, Inc. All Rights Reserved. *


* *


* DISCLAIMER: The authors and publisher of this book have used their *


* best efforts in p
reparing the book. These efforts include the *


* development, research, and testing of the theories and programs *


* to determine their effectiveness. The authors and publisher make *


* no warranty of any kind, expressed or implied
, with regard to these *


* programs or to the documentation contained in these books. The authors *


* and publisher shall not be liable in any event for incidental or *


* consequential damages in connection with, or arising out of, the *


* furnishing, performance, or use of these programs. *


*************************************************************************/



import

java.util.Arrays;

import

java.util.Random;


public

class

SimpleArray

{


private

final

char
[]
array
;
// the shared integer array


private

int

writeIndex

= 0;
// index of next element to be written


private

final

static

Random
generator

=
new

Random();



// construct a SimpleArray of a given size


public

SimpleArray(
int

size )


{


ar
ray

=
new

char
[ size ];


}
// end constructor



// add a value to the shared array


public

synchronized

void

add(
char

value )


{



if
(value <
'a'
) value=
'a'
;



value = (
char
) (((value
-

'a'
) % (
'z'

-

'a'

+ 1) )+
'a'
);


int

position =
writeIndex
;
// store the write index



try


{


// put thread to sleep for 0
-
499 milliseconds


Thread.
sleep
(
generator
.nextInt( 500 ) );


}
// end try


catch

( InterruptedException ex )


{


ex.printStackTrace();


}
// end catch



// put value in the appropriate element


array
[ position ] = value;


System.
out
.printf(
"%s wrote '%c' to element %d.
\
n"
,


Thread.
currentThread
().getName(), value, position );




++
writeIndex
;
// increment index of element to be written next


System.
out
.printf(
"Next write index: %d
\
n"
,
writeIndex

);


}
// end method add




// used for outputting the contents of the shared integer array


public

String toString()


{


String arrayString =
"
\
nContents of SimpleArray:
\
n"
;




for

(
int

i = 0; i <
array
.
length
; i++ )


arrayString +=
array
[ i ] +
" "
;




return

arrayString;


}
// end method toString

}
// end class SimpleArray


Questions:

1.

How the char
array ‘array’ is being shared?

2.

Is there any other method to generate random numbers other then
nextInt() method?



/**************************************************************************


* (C) Copyright 1992
-
2010 by Deitel & Associates, I
nc. and *


* Pearson Education, Inc. All Rights Reserved. *


* *


* DISCLAIMER: The authors and publisher of this book have used their *


* b
est efforts in preparing the book. These efforts include the *


* development, research, and testing of the theories and programs *


* to determine their effectiveness. The authors and publisher make *


* no warranty of any kind, expr
essed or implied, with regard to these *


* programs or to the documentation contained in these books. The authors *


* and publisher shall not be liable in any event for incidental or *


* consequential damages in connection with, or arising out
of, the *


* furnishing, performance, or use of these programs. *


*************************************************************************/



3)

import

java.lang.Runnable;


public

class

ArrayWriter
implements

Runnable {


private

final

SimpleArray
sharedSimpleArray
;


private

final

char

startValue
;



public

ArrayWriter(
char

value, SimpleArray array) {


startValue

= value;


sharedSimpleArray

= array;


}
// end constructor



public

void

run() {


for

(
int

i = 0; i < 3; i++) {


byte

byteValue = (
byte
)
startValue
;


sharedSimpleArray
.add((
char
) (byteValue + i));


}
// end for


}
// end method run

}
// end class ArrayWriter


Questions:

1.

What does
((
char
) (
byteValue + i)) do?

2.

When the run() method is called?



/**************************************************************************


* (C) Copyright 1992
-
2007 by Deitel & Associates, Inc. and * Pearson
Education,


* Inc. All Rights Reserved. * * DISCLAIMER:

The authors and publisher of
this


* book have used their * best efforts in preparing the book. These efforts


* include the * development, research, and testing of the theories and
programs


* * to determine their effectiveness. The authors and publisher

make * no


* warranty of any kind, expressed or implied, with regard to these *
programs


* or to the documentation contained in these books. The authors * and
publisher


* shall not be liable in any event for incidental or * consequential
damages in


* c
onnection with, or arising out of, the * furnishing, performance, or
use of


* these programs. *


*************************************************************************/



import

java.util.concurrent.Executors;

import

java.util.concurrent.ExecutorServic
e;

import

java.util.concurrent.TimeUnit;


public

class

SharedArrayTest

{


public

static

void

main( String[] arg )


{


// construct the shared object


SimpleArray sharedSimpleArray =
new

SimpleArray( 6 );



// create two tasks to write to

the shared SimpleArray


ArrayWriter writer1 =
new

ArrayWriter(
'a'
, sharedSimpleArray );


ArrayWriter writer2 =
new

ArrayWriter(
'd'
, sharedSimpleArray );



// execute the tasks with an ExecutorService


ExecutorService executor = Execu
tors.
newCachedThreadPool
();


executor.execute( writer1 );


executor.execute( writer2 );



executor.shutdown();



try


{


// wait 1 minute for both writers to finish executing


boolean

tasksEnded = executor.awaitTermination(


1, TimeUnit.
MINUTES

);



if

( tasksEnded )


System.
out
.println( sharedSimpleArray );
// print contents


else


System.
out
.println(


"Timed out while w
aiting for tasks to finish."

);


}
// end try


catch

( InterruptedException ex )


{


System.
out
.println(


"Interrupted while wait for tasks to finish."

);


}
// end catch


}
// end main

}
// end class
SharedArrayTest


Questions:

1.

What is
TimeUnit.
MINUTES
?

2.

What is ExecutorService?



/**************************************************************************


* (C) Copyright 1992
-
2007 by Deitel & Associates, Inc. and *


* Pearson Education, I
nc. All Rights Reserved. *


* *


* DISCLAIMER: The authors and publisher of this book have used their *


* best efforts in preparing the book. These effort
s include the *


* development, research, and testing of the theories and programs *


* to determine their effectiveness. The authors and publisher make *


* no warranty of any kind, expressed or implied, with regard to these *


*
programs or to the documentation contained in these books. The authors *


* and publisher shall not be liable in any event for incidental or *


* consequential damages in connection with, or arising out of, the *


* furnishing, performance, or use of these programs. *


*************************************************************************/




import

java.util.Random;


public

class

SimpleArray

{


private

final

char

array
[];
// the shared int
eger array


private

int

writeIndex

= 0;
// index of next element to be written


private

final

static

Random
generator

=
new

Random();



// construct a SimpleArray of a given size


public

SimpleArray(
int

size )


{


array

=
new

char
[ size ];


}
// end constructor



// add a value to the shared array


public

synchronized

void

add(
char

value )


{


int

position =
writeIndex
;
// store the write index



try


{


// put thread to sleep for 0
-
499 milliseconds


Thread.
sleep
(
generator
.nextInt( 500 ) );


}
// end try


catch

( InterruptedException ex )


{


ex.printStackTrace();


}
// end catch



// put value in the appropriate element


array
[ position ] = value;


System.
out
.printf(
"%s wrote %s to element %d.
\
n"
,


Thread.
currentThread
().getName(), value, position );



++
writeIndex
;
// increment index of element to be written next


System.
out
.printf(
"Next write index: %d
\
n"
,
writeIndex

);


}
// end method add




// used for outputting the contents of the shared integer array


public

String toString()


{


String arrayString =
"
\
nContents of SimpleArray:
\
n"
;




for

(
int

i = 0; i <
array
.
length
; i++ )


arrayStrin
g +=
array
[ i ] +
" "
;




return

arrayString;


}
// end method toString

}
// end class SimpleArray


Questions:

1.

How to set the name of the thread
?

2.

Is there any other method to generate random numbers other then
nextInt() method?



/**************************************************************************


* (C) Copyright 1992
-
2007 by Deitel & Associates, Inc. and *


* Pearson Education, Inc. All Rights Reserved. *


*

*


* DISCLAIMER: The authors and publisher of this book have used their *


* best efforts in preparing the book. These efforts include the *


* development, research, and testing of the theories an
d programs *


* to determine their effectiveness. The authors and publisher make *


* no warranty of any kind, expressed or implied, with regard to these *


* programs or to the documentation contained in these books. The authors *


* and p
ublisher shall not be liable in any event for incidental or *


* consequential damages in connection with, or arising out of, the *


* furnishing, performance, or use of these programs. *




4)

Synchronized

import

java.lang.Runnable;


public

class

ArrayWriter
implements

Runnable

{


private

final

SimpleArray
sharedSimpleArray
;

// private final int startValue;


private

final

char
[]
startValue
;


// public ArrayWriter( int value, SimpleArray array )


public

A
rrayWriter( String value, SimpleArray array )


{

// startValue = value;


startValue

= value.toCharArray();


sharedSimpleArray
= array;


}
// end constructor



public

void

run()


{

// for ( int i = startValue; i < startValue + 3;
i++ )


for

(
char

oneChar :
startValue
)


{

// sharedSimpleArray.add( i ); // add an element to the shared
array


sharedSimpleArray
.add( oneChar );
// add an element to the shared
array


}
// end for


}
// end method run

}
//
end class ArrayWriter



Questions:

1.

How to set the name of the thread while Implementing Runnable
interface
?

2.

Explain
char

oneChar :
startValue loop.




/**************************************************************************


* (C) Copyright 1992
-
2007 by

Deitel & Associates, Inc. and *


* Pearson Education, Inc. All Rights Reserved. *


* *


* DISCLAIMER: The authors and publisher of this book hav
e used their *


* best efforts in preparing the book. These efforts include the *


* development, research, and testing of the theories and programs *


* to determine their effectiveness. The authors and publisher make *


* no war
ranty of any kind, expressed or implied, with regard to these *


* programs or to the documentation contained in these books. The authors *


* and publisher shall not be liable in any event for incidental or *


* consequential damages in connectio
n with, or arising out of, the *


* furnishing, performance, or use of these programs. *


*************************************************************************/



import

java.util.concurrent.Executors;

import

java.util.concurrent.ExecutorService;

import

java.util.concurrent.TimeUnit;


public

class

SharedArrayTest

{


public

static

void

main( String[] arg )


{


// construct the shared object


SimpleArray sharedSimpleArray =
new

SimpleArray( 6 );




// create two tasks to write to the shared SimpleArray

// ArrayWriter writer1 = new ArrayWriter( 1, sharedSimpleArray );

// ArrayWriter writer2 = new ArrayWriter( 11, sharedSimpleArray );


ArrayWriter writer1 =
new

ArrayWriter(
"abc"
, s
haredSimpleArray );


ArrayWriter writer2 =
new

ArrayWriter(
"xyz"
, sharedSimpleArray );



// execute the tasks with an ExecutorService


ExecutorService executor = Executors.
newCachedThreadPool
();


executor.execute( writer1 );


executor.execute( writer2 );



executor.shutdown();



try


{


// wait 1 minute for both writers to finish executing


boolean

tasksEnded = executor.awaitTermination(


1, TimeUnit.
MINUTES

);



if

( tasksEnded )


System.
out
.println( sharedSimpleArray );
// print contents


else


System.
out
.println(


"Timed out while waiting for tasks to finish."

);


}
// end try


catch

( InterruptedException ex
)


{


System.
out
.println(


"Interrupted while wait for tasks to finish."

);


}
// end catch


}
// end main

}
// end class SharedArrayTest


Questions:

1.

What
String[] arg is passed in main method?

2.

What is ExecutorService?



/**************************************************************************


* (C) Copyright 1992
-
2007 by Deitel & Associates, Inc. and *


* Pearson Education, Inc. All Rights Reserved. *


*

*


* DISCLAIMER: The authors and publisher of this book have used their *


* best efforts in preparing the book. These efforts include the *


* development, research, and testing of the theories and programs *


* to determine their effectiveness. The authors and publisher make *


* no warranty of any kind, expressed or implied, with regard to these *


* programs or to the docume
ntation contained in these books. The authors *


* and publisher shall not be liable in any event for incidental or *


* consequential damages in connection with, or arising out of, the *


* furnishing, performance, or use of these programs. *


*************************************************************************/




import

java.util.Random;


public

class

SimpleArray

{

// private final int array[]; // the shared in
teger array


private

final

char

array
[];
// the shared character array


private

int

writeIndex

= 0;
// index of next element to be written


private

final

static

Random
generator

=
new

Random();



// construct a SimpleArray of a given size


public

SimpleArray(
int

size )


{

// array = new int[ size ];


array

=
new

char
[ size ];


}
// end constructor



// add a value to the shared array

// public synchronized void add( int value )


public

synchronized

void

add(
char

value )


{


int

position =
writeIndex
;
// store the write index



try


{


// put thread to sleep for 0
-
499 milliseconds


Thread.
sleep
(
generator
.nextInt( 500 ) );


}
// end try


catch

( InterruptedException ex )


{


ex.printStackTrace();


}
// end catch



// put value in the appropriate element


array
[ position ] = value;

// System.out.printf( "%s wrote %2d to element %d.
\
n",


System.
out
.printf(
"%s wrote '%s' to element %d.
\
n"
,



Thread.
currentThread
().getName(), value, position );



++
writeIndex
;
// increment index of element to be written next


System.
out
.printf(
"Next write index: %d
\
n"
,
writeIndex

);


}
// end method add




// used for outputting the
contents of the shared integer array


public

String toString()


{


String arrayString =
"
\
nContents of SimpleArray:
\
n"
;




for

(
int

i = 0; i <
array
.
length
; i++ )


arrayString +=
array
[ i ] +
" "
;




return

arrayString;


}
// end method toString

}
// end class SimpleArray


Questions:

1.

How to set the name of the thread
?

2.

Is there any other method to generate random numbers other then
nextInt() method?



/*********************************************************************
*****


* (C) Copyright 1992
-
2007 by Deitel & Associates, Inc. and *


* Pearson Education, Inc. All Rights Reserved. *


* *


* DISCLAIMER: The aut
hors and publisher of this book have used their *


* best efforts in preparing the book. These efforts include the *


* development, research, and testing of the theories and programs *


* to determine their effectiveness. The authors and publisher make *


* no warranty of any kind, expressed or implied, with regard to these *


* programs or to the documentation contained in these books. The authors *


* and publisher shall not b
e liable in any event for incidental or *


* consequential damages in connection with, or arising out of, the *


* furnishing, performance, or use of these programs. *


*************************************************************************/



UN
Synchronized

import

java.lang.Runnable;


public

class

ArrayWriter
implements

Runnable

{


private

final

SimpleArray
sharedSimpleArray
;

// private final int startValue;


private

final

char
[]
startValue
;


// public ArrayWriter( int value, SimpleArray array )


public

ArrayWriter( String value, SimpleArray array )


{

// startValue = value;


startValue

= value.toCharArray();


sharedSimpleArray
= array;


}

// end constructor



public

void

run()


{

// for ( int i = startValue; i < startValue + 3; i++ )


for

(
char

oneChar :
startValue
)


{

// sharedSimpleArray.add( i ); // add an element to the shared
array


sharedSimpleArray
.add( oneChar );
// add an element to the shared
array


}
// end for


}
// end method run

}
// end class ArrayWriter



Questions:

1.

When the run method is called?

2.

Explain
char

oneChar :
startValue loop?




/**************************************************************************


* (C) Copyright 1992
-
2007 by Deitel & Associates, Inc. and *


* Pearson Education, Inc. All Rights Reserved. *


*

*


* DISCLAIMER: The authors and publisher of this book have used their *


* best efforts in preparing the book. These efforts include the *


* development, research, and testing of the theories and programs *


* to determine their effectiveness. The authors and publisher make *


* no warranty of any kind, expressed or implied, with regard to these *


* programs or to the docume
ntation contained in these books. The authors *


* and publisher shall not be liable in any event for incidental or *


* consequential damages in connection with, or arising out of, the *


* furnishing, performance, or use of these programs.

*


*************************************************************************/




import

java.util.concurrent.Executors;

import

java.util.concurrent.ExecutorService;

import

java.util.concurrent.TimeUnit;


public

class

SharedArrayTest

{


public

static

void

main( String[] arg )


{


// construct the shared object


SimpleArray sharedSimpleArray =
new

SimpleArray( 6 );



// create two tasks to write to the shared SimpleArray

// ArrayWriter writer1 = new ArrayWriter( 1, sh
aredSimpleArray );

// ArrayWriter writer2 = new ArrayWriter( 11, sharedSimpleArray );


ArrayWriter writer1 =
new

ArrayWriter(
"abc"
, sharedSimpleArray );


ArrayWriter writer2 =
new

ArrayWriter(
"zyx"
, sharedSimpleArray );



// execute
the tasks with an ExecutorService


ExecutorService executor = Executors.
newCachedThreadPool
();


executor.execute( writer1 );


executor.execute( writer2 );



executor.shutdown();



try


{


// wait 1 minute for both
writers to finish executing


boolean

tasksEnded = executor.awaitTermination(


1, TimeUnit.
MINUTES

);



if

( tasksEnded )


System.
out
.println( sharedSimpleArray );
// print contents


else


System.
out
.println(


"Timed out while waiting for tasks to finish."

);


}
// end try


catch

( InterruptedException ex )


{


System.
out
.println(


"Interrupted while wait for tasks to finish."

);


}
// end catch


}
// end main

}
// end class SharedArrayTest


Questions:

1.

What is
ExecutorService?

2.

What are other units then minutes in
TimeUnit.
MINUTES
?



/**************************************************************************


* (C) Copyright 1992
-
2007 by Deitel &

Associates, Inc. and *


* Pearson Education, Inc. All Rights Reserved. *


* *


* DISCLAIMER: The authors and publisher of this book have used th
eir *


* best efforts in preparing the book. These efforts include the *


* development, research, and testing of the theories and programs *


* to determine their effectiveness. The authors and publisher make *


* no warranty of
any kind, expressed or implied, with regard to these *


* programs or to the documentation contained in these books. The authors *


* and publisher shall not be liable in any event for incidental or *


* consequential damages in connection with, o
r arising out of, the *


* furnishing, performance, or use of these programs. *


*************************************************************************/



import

java.util.Random;


public

class

SimpleArray
// CAUTION: NOT
THREAD SAFE!

{

// private final int array[]; // the shared integer array


private

final

char

array
[];
// the shared character array


private

int

writeIndex

= 0;
// index of next element to be written


private

final

static

Random
generator

=
new

Random();



// construct a SimpleArray of a given size


public

SimpleArray(
int

size )


{

// array = new int[ size ];


array

=
new

char
[ size ];


}
// end constructor



// add a value to the shared array

// public void add( int value

)


public

void

add(
char

value )


{


int

position =
writeIndex
;
// store the write index



try


{


// put thread to sleep for 0
-
499 milliseconds


Thread.
sleep
(
generator
.nextInt( 500 ) );


}
// end try


catch

( InterruptedException ex )


{


ex.printStackTrace();


}
// end catch



// put value in the appropriate element


array
[ position ] = value;

// System.out.printf( "%s wrote %2d to element %d.
\
n",


System.
out
.printf(
"%s wrote '%s' to element %d.
\
n"
,


Thread.
currentThread
().getName(), value, position );



++
writeIndex
;
// increment index of element to be written next


System.
out
.printf(
"Next write index: %d
\
n"
,
writeIndex

);


}
// end method add





// used for outputting the contents of the shared integer array


public

String toString()


{


String arrayString =
"
\
nContents of SimpleArray:
\
n"
;




for

(
int

i = 0; i <
array
.
length
; i++ )


arrayString +=
array
[ i ] +
" "
;




return

arrayString;


}
// end method toString

}
// end class SimpleArray


Questions:

1.

In what conditions
InterruptedException may occur?

2.

What does
printStackTrace do?




/**************************************************************************


* (C) Copyright 1992
-
2007 by Deitel & Associates, Inc. and *


* Pearson Education, Inc. All Rights Reserved. *


* *


* DISCLAIMER: The authors
and publisher of this book have used their *


* best efforts in preparing the book. These efforts include the *


* development, research, and testing of the theories and programs *


* to determine their effectiveness. The authors and pu
blisher make *


* no warranty of any kind, expressed or implied, with regard to these *


* programs or to the documentation contained in these books. The authors *


* and publisher shall not be liable in any event for incidental or *


* cons
equential damages in connection with, or arising out of, the *


* furnishing, performance, or use of these programs. *


*************************************************************************/