📄 sharedcell2.java
字号:
// Show multiple threads modifying shared object.
import java.text.DecimalFormat;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class SharedCell extends JFrame
{
public SharedCell()
{
super( "Demonstrating Thread Synchronization" );
JTextArea output = new JTextArea( 20, 30 );
getContentPane().add( new JScrollPane( output ) );
setSize( 500, 500 );
show();
// set up threads and start threads
HoldIntegerSynchronized h = new HoldIntegerSynchronized( output );
ProduceInteger p = new ProduceInteger( h, output );
ConsumeInteger c = new ConsumeInteger( h, output );
p.start();
c.start();
}
public static void main( String args[] )
{
SharedCell app = new SharedCell();
app.addWindowListener( new WindowAdapter()
{
public void windowClosing( WindowEvent e )
{
System.exit( 0 );
}
} );
}
}
// Definition of threaded class ProduceInteger
import javax.swing.JTextArea;
public class ProduceInteger extends Thread
{
private HoldIntegerSynchronized pHold;
private JTextArea output;
public ProduceInteger( HoldIntegerSynchronized h, JTextArea o )
{
super( "ProduceInteger" );
pHold = h;
output = o;
}
public void run()
{
for ( int count = 1; count <= 10; count++ )
{
// sleep for a random interval
// Note: Interval shortened purposely to fill buffer
try
{
Thread.sleep( (int) ( Math.random() * 500 ) );
}
catch( InterruptedException e )
{
System.err.println( e.toString() );
}
pHold.setSharedInt( count );
}
output.append( "\n" + getName() + " finished producing values" + "\nTerminating " + getName() + "\n" );
}
}
// Definition of threaded class ConsumeInteger
import javax.swing.JTextArea;
public class ConsumeInteger extends Thread
{
private HoldIntegerSynchronized cHold;
private JTextArea output;
public ConsumeInteger( HoldIntegerSynchronized h, JTextArea o )
{
super( "ConsumeInteger" );
cHold = h;
output = o;
}
public void run()
{
int val, sum = 0;
do
{
// sleep for a random interval
try
{
Thread.sleep( (int) ( Math.random() * 3000 ) );
}
catch( InterruptedException e )
{
System.err.println( e.toString() );
}
val = cHold.getSharedInt();
sum += val;
}
while ( val != 10 );
output.append( "\n" + getName() + " retrieved values totaling: " + sum + "\nTerminating " + getName() + "\n" );
}
}
// Definition of class HoldIntegerSynchronized that
// uses thread synchronization to ensure that both
// threads access sharedInt at the proper times.
import javax.swing.JTextArea;
import java.text.DecimalFormat;
public class HoldIntegerSynchronized
{
private int sharedInt[] = { -1, -1, -1, -1, -1 };
private boolean writeable = true;
private boolean readable = false;
private int readLoc = 0, writeLoc = 0;
private JTextArea output;
public HoldIntegerSynchronized( JTextArea o )
{
output = o;
}
public synchronized void setSharedInt( int val )
{
while ( !writeable )
{
try
{
output.append( " WAITING TO PRODUCE " + val );
wait();
}
catch ( InterruptedException e )
{
System.err.println( e.toString() );
}
}
sharedInt[ writeLoc ] = val;
readable = true;
output.append( "\nProduced " + val + " into cell " + writeLoc );
writeLoc = ( writeLoc + 1 ) % 5;
output.append( "\twrite " + writeLoc + "\tread " + readLoc);
displayBuffer( output, sharedInt );
if ( writeLoc == readLoc )
{
writeable = false;
output.append( "\nBUFFER FULL" );
}
notify();
}
public synchronized int getSharedInt()
{
int val;
while ( !readable )
{
try
{
output.append( " WAITING TO CONSUME" );
wait();
}
catch ( InterruptedException e )
{
System.err.println( e.toString() );
}
}
writeable = true;
val = sharedInt[ readLoc ];
output.append( "\nConsumed " + val + " from cell " + readLoc );
readLoc = ( readLoc + 1 ) % 5;
output.append( "\twrite " + writeLoc + "\tread " + readLoc );
displayBuffer( output, sharedInt );
if ( readLoc == writeLoc )
{
readable = false;
output.append( "\nBUFFER EMPTY" );
}
notify();
return val;
}
public void displayBuffer( JTextArea out, int buf[] )
{
DecimalFormat formatNumber = new DecimalFormat( " #;-#" );
output.append( "\tbuffer: " );
for ( int i = 0; i < buf.length; i++ )
out.append( " " + formatNumber.format( buf[ i ] ));
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -