Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Java - Threads b b A thread is an individual flow of control within a larger program. b b A program which is running more than one thread is said to.

Similar presentations


Presentation on theme: "1 Java - Threads b b A thread is an individual flow of control within a larger program. b b A program which is running more than one thread is said to."— Presentation transcript:

1 1 Java - Threads b b A thread is an individual flow of control within a larger program. b b A program which is running more than one thread is said to be multithreaded. b b You can think of threads as the ability of your program to do multiple independent actions at the same time where each action runs in its own thread. Each thread can be controlled (started, stopped, prioritised) individually.

2 2 Threads b b Unlike separate programs, threads within the same class share data members making it easy for them to work co-operatively.

3 3 No Threads import java.awt.Graphics; import java.util.Date; public class Clock extends java.applet.Applet

4 4 No Threads public void start( ) { while ( true ) { repaint( ); try {clockThread.sleep( 1000 );} catch ( Exception e ) {} catch ( Exception e ) {}}}

5 5 No Threads public void paint( Graphics g ) { Date now = new Date( ); g.drawString(now.getHours( ) + ":" + now.getMinutes( )+ ":" + now.getSeconds( ), 5,10); }

6 6 Threads import java.awt.Graphics; import java.util.Date; public class Clock extends java.applet.Applet implements Runnable { Thread clockThread;

7 7 Threads public void start( ) { if ( clockThread == null ) { clockThread = new Thread ( this, "Clock" ); clockThread.start( ); }}

8 8 Threads public void run( ) { while ( true ) { repaint( ); try {clockThread.sleep( 1000 );} catch ( Exception e ) {} catch ( Exception e ) {}}}

9 9 Threads public void paint( Graphics g ) { Date now = new Date( ); g.drawString(now.getHours( ) + ":" + now.getMinutes( )+ ":" + now.getSeconds( ), 5,10); }

10 10 Threads public void stop( ) { clockThread.stop( ); clockThread = null; }}

11 11 Control Structures if(grade >= 16) system.out.println( "passed"); else system.out.println( "failed");

12 12 Control Structures switch(grade) { case 'A': gradeA++; break; case 'B': gradeB++; break;

13 13 Control Structures case 'C': gradeC++; break; case 'D': gradeD++; break; default: showStatus("invalid grade. Enter new grade.");

14 14 Repetition Structures b b While   do/while b b for

15 15 Repetition Structures int i = 2; while(i <=100) i = i + 1;

16 16 Repetition Structures for(int i = 2; i <= 100; i++) j = j + 1;

17 17 Repetition Structures int i = 2; do { i = i + 1; } while(i <=100)

18 18 Arrays b b Creating an array: MyObject arrayofobjs[] = new MyObject[10]; b b add actual MyObject objects to that array: for (int i=0; i < arrayofobjs.length; i++) arrayofobjs[i] = new MyObject;

19 19 Creating Arrays - examples String[] string Array; int[] intArray; boolean[] booleanArray; int[] my IntArray = new int[20]; int[] numbers = {2, 5, 8, 10};

20 20 Input/output b Files and Streams System.in - standard input stream objectSystem.in - standard input stream object System.out - standard output stream objectSystem.out - standard output stream object System.err - standard input stream objectSystem.err - standard input stream object are three stream object created when a Java program is executed.

21 21 Input/output b To perform file processing in Java java.io package must be imported. b Stream Classes FileInputStrems - for input from a fileFileInputStrems - for input from a file FileOutputStream - for output to a fileFileOutputStream - for output to a file

22 22 Object InputStreamOutputStream FileInputStream FileOutputStream PipedInputStream PipedOutputStream Class Hierarchy

23 23 More on Class Hierarchy b Object FileFile FileDescriptorFileDescriptor StreamTokenizerStreamTokenizer InputStreamInputStream OutputStreamOutputStream RandomAccessFileRandomAccessFile

24 24 More on Class Hierarchy b InputStream ByteArrayInputStreamByteArrayInputStream SequenceInputStreamSequenceInputStream StringBufferInputStreamStringBufferInputStream PipedInputStreamPipedInputStream FileInputStreamFileInputStream FilterInputStreamFilterInputStream –DataInputStream –BufferedInputStream –PushBackInputStream –LineNumberInputStream

25 25 More on Class Hierarchy b OutputStream ByteArrayOutputStreamByteArrayOutputStream PipedOutputStreamPipedOutputStream FileOutputStreamFileOutputStream FilterOutputStreamFilterOutputStream –DataOutputStream –BufferedOutputStream –PrintStream

26 26 Input/output b Pipes synchronised communication channels between threadssynchronised communication channels between threads One thread send data to another by writing to a PipedOutputStream.One thread send data to another by writing to a PipedOutputStream. The target thread reads information from the pipe via a PipedInputStreamThe target thread reads information from the pipe via a PipedInputStream

27 27 Creating a FileOutputStream object and attempting to open a file: try { output = new DataOutputStream( output = new DataOutputStream( new FileOutputStream(“afile.dat”)); } catch ( IOException e ) { System.err.println(“File not opened properly\n” + e.toString()); System.err.println(“File not opened properly\n” + e.toString()); System.exit( 1 );}

28 28 b Writing to a file output.writeInt ( accountNumber); output.writeDouble(doubleValue); b Closing the file output.close();

29 29 Opening a file to read try { input = new DataInputStream( input = new DataInputStream( new FileInputStream(“afile.dat”)); } catch ( IOException e ) { System.err.println(“File not opened properly\n” + e.toString()); System.err.println(“File not opened properly\n” + e.toString()); System.exit( 1 );}

30 30 Reading from a file Account = input.readInt(); balance = input.readDouble(); input.close();

31 31 Read/Write b And of course System.out.print(“Enter grade:”); grade = System.in.read();


Download ppt "1 Java - Threads b b A thread is an individual flow of control within a larger program. b b A program which is running more than one thread is said to."

Similar presentations


Ads by Google