🗊Презентация Multithreading IO Streams Java Core

Нажмите для полного просмотра!
Multithreading IO Streams Java Core, слайд №1Multithreading IO Streams Java Core, слайд №2Multithreading IO Streams Java Core, слайд №3Multithreading IO Streams Java Core, слайд №4Multithreading IO Streams Java Core, слайд №5Multithreading IO Streams Java Core, слайд №6Multithreading IO Streams Java Core, слайд №7Multithreading IO Streams Java Core, слайд №8Multithreading IO Streams Java Core, слайд №9Multithreading IO Streams Java Core, слайд №10Multithreading IO Streams Java Core, слайд №11Multithreading IO Streams Java Core, слайд №12Multithreading IO Streams Java Core, слайд №13Multithreading IO Streams Java Core, слайд №14Multithreading IO Streams Java Core, слайд №15Multithreading IO Streams Java Core, слайд №16Multithreading IO Streams Java Core, слайд №17Multithreading IO Streams Java Core, слайд №18Multithreading IO Streams Java Core, слайд №19Multithreading IO Streams Java Core, слайд №20Multithreading IO Streams Java Core, слайд №21Multithreading IO Streams Java Core, слайд №22Multithreading IO Streams Java Core, слайд №23Multithreading IO Streams Java Core, слайд №24Multithreading IO Streams Java Core, слайд №25Multithreading IO Streams Java Core, слайд №26Multithreading IO Streams Java Core, слайд №27Multithreading IO Streams Java Core, слайд №28Multithreading IO Streams Java Core, слайд №29Multithreading IO Streams Java Core, слайд №30Multithreading IO Streams Java Core, слайд №31Multithreading IO Streams Java Core, слайд №32Multithreading IO Streams Java Core, слайд №33Multithreading IO Streams Java Core, слайд №34Multithreading IO Streams Java Core, слайд №35Multithreading IO Streams Java Core, слайд №36Multithreading IO Streams Java Core, слайд №37Multithreading IO Streams Java Core, слайд №38Multithreading IO Streams Java Core, слайд №39Multithreading IO Streams Java Core, слайд №40

Содержание

Вы можете ознакомиться и скачать презентацию на тему Multithreading IO Streams Java Core. Доклад-сообщение содержит 40 слайдов. Презентации для любого класса можно скачать бесплатно. Если материал и наш сайт презентаций Mypresentation Вам понравились – поделитесь им с друзьями с помощью социальных кнопок и добавьте в закладки в своем браузере.

Слайды и текст этой презентации


Слайд 1





Multithreading 
IO Streams
Java Core
Описание слайда:
Multithreading IO Streams Java Core

Слайд 2





Agenda
Processes and Threads
Threads in Java
Java Input and Output Streams
File Input/Output streams
Practical tasks
Описание слайда:
Agenda Processes and Threads Threads in Java Java Input and Output Streams File Input/Output streams Practical tasks

Слайд 3





What exactly is a concurrent ?
Описание слайда:
What exactly is a concurrent ?

Слайд 4





Processes and Threads
Process is a set of threads within process’ address space
Each thread has its own set of CPU registers, called the thread's context. 
The context reflects the state of the thread's CPU registers when the thread last executed.
Описание слайда:
Processes and Threads Process is a set of threads within process’ address space Each thread has its own set of CPU registers, called the thread's context. The context reflects the state of the thread's CPU registers when the thread last executed.

Слайд 5





How to create new Thread ?
Описание слайда:
How to create new Thread ?

Слайд 6


Multithreading IO Streams Java Core, слайд №6
Описание слайда:

Слайд 7





Threads in Java
Java Virtual Machines support multithreading.
Thread of execution in Java is an instance of class Thread. In order to write thread of excecution the class must inherit from this class and override the method run().
  public class MyThread extends Thread {
     public void run( ) {
        // a long operation, calculation
        long sum = 0;
        for (int i = 0; i < 1000; i++) {
           sum += i;
        }
        System.out.println(sum);
     }
  }
Описание слайда:
Threads in Java Java Virtual Machines support multithreading. Thread of execution in Java is an instance of class Thread. In order to write thread of excecution the class must inherit from this class and override the method run(). public class MyThread extends Thread { public void run( ) { // a long operation, calculation long sum = 0; for (int i = 0; i < 1000; i++) { sum += i; } System.out.println(sum); } }

Слайд 8





Threads in Java
To start a thread, you must create an instance of a derived class and call the inherited method start().
   MyThread t = new MyThread( );
   t.start( );
   public class MyThread extends Thread {
      public void run( ) {
      // …
      }
   }
Описание слайда:
Threads in Java To start a thread, you must create an instance of a derived class and call the inherited method start(). MyThread t = new MyThread( ); t.start( ); public class MyThread extends Thread { public void run( ) { // … } }

Слайд 9





Threads in Java
Since Java does not use multiple inheritance, the requirement to inherit from the Thread can lead to conflict.
Sufficiently to implement an interface Runnable, which declared the method void run()
Описание слайда:
Threads in Java Since Java does not use multiple inheritance, the requirement to inherit from the Thread can lead to conflict. Sufficiently to implement an interface Runnable, which declared the method void run()

Слайд 10





Thread life cycle
Описание слайда:
Thread life cycle

Слайд 11





State cycle
Описание слайда:
State cycle

Слайд 12





How to control threads ???
Описание слайда:
How to control threads ???

Слайд 13





Threads in Java
public class MyThread extends Thread {
   private int number;
   private int pause;
   public MyThread(int number, int pause) {
      this.number = number;
      this.pause = pause;
   }
   @Override
   public void run() {
      for (int i = 0; i < 5; i++) {
         try { sleep(pause); 
         } catch (InterruptedException e) {}
         System.out.println("Thread " + number);
      }
}  }
Описание слайда:
Threads in Java public class MyThread extends Thread { private int number; private int pause; public MyThread(int number, int pause) { this.number = number; this.pause = pause; } @Override public void run() { for (int i = 0; i < 5; i++) { try { sleep(pause); } catch (InterruptedException e) {} System.out.println("Thread " + number); } } }

Слайд 14





Threads in Java
public class Example {
   public static void main(String[] args) throws Exception {
      Thread t1 = new MyThread(1, 100);
      Thread t2 = new MyThread(2, 250);
      t1.start();
      t2.start();
      // t1.join();
      // t2.join();
      System.out.println("Thread main");
   }
}
Описание слайда:
Threads in Java public class Example { public static void main(String[] args) throws Exception { Thread t1 = new MyThread(1, 100); Thread t2 = new MyThread(2, 250); t1.start(); t2.start(); // t1.join(); // t2.join(); System.out.println("Thread main"); } }

Слайд 15





Threads in Java
Also we can change the procedure to start the stream.
  
Thread t[ ] = new Thread[3];
Описание слайда:
Threads in Java Also we can change the procedure to start the stream. Thread t[ ] = new Thread[3];

Слайд 16





Example
Описание слайда:
Example

Слайд 17





Example
public class Appl {
    public static int sum = 0;
    public static void main(String[ ] args) {
        Runnable r1 = new Run1( );
        Thread t1 = new Thread(r1);
        Runnable r2 = new Run2( );
        Thread t2 = new Thread(r2);
        t1.start( );
        t2.start( );
	  Thread.yield( );
        System.out.println("Success, sum = " + sum);
    }
}
Описание слайда:
Example public class Appl { public static int sum = 0; public static void main(String[ ] args) { Runnable r1 = new Run1( ); Thread t1 = new Thread(r1); Runnable r2 = new Run2( ); Thread t2 = new Thread(r2); t1.start( ); t2.start( ); Thread.yield( ); System.out.println("Success, sum = " + sum); } }

Слайд 18





Synchronization in Java
Описание слайда:
Synchronization in Java

Слайд 19





Synchronized
The keyword synchronized can be applied in two variants – to declare a synchronized-block and as a modifier of the method.
If another thread has already installed a lock on object, the execution of the first stream is suspended. After this block it’s executed.
Описание слайда:
Synchronized The keyword synchronized can be applied in two variants – to declare a synchronized-block and as a modifier of the method. If another thread has already installed a lock on object, the execution of the first stream is suspended. After this block it’s executed.

Слайд 20





Synchronized
Описание слайда:
Synchronized

Слайд 21





Deadlock
When working with locks the possible appearance of deadlock should always be remembered – deadlock, which leads to stop responding the program. 
public class DeadlockDemo {
public final static Object first = new Object();
public final static Object second = new Object();
public static void main(String s[]) {
   Thread t1 = new Thread() {
   public void run() {
      synchronized (first) {
         Thread.yield();
         synchronized (second) {
            System.out.println("Success!");
   }  }  }  };
Описание слайда:
Deadlock When working with locks the possible appearance of deadlock should always be remembered – deadlock, which leads to stop responding the program. public class DeadlockDemo { public final static Object first = new Object(); public final static Object second = new Object(); public static void main(String s[]) { Thread t1 = new Thread() { public void run() { synchronized (first) { Thread.yield(); synchronized (second) { System.out.println("Success!"); } } } };

Слайд 22





Threads in Java
Thread t2 = new Thread() {
   public void run() {
      synchronized (second) {
         Thread.yield();
         synchronized (first) {
            System.out.println("Success!");
         }
      }
   }
};
t1.start();
t2.start();
}
}
Описание слайда:
Threads in Java Thread t2 = new Thread() { public void run() { synchronized (second) { Thread.yield(); synchronized (first) { System.out.println("Success!"); } } } }; t1.start(); t2.start(); } }

Слайд 23





wait()   notify()   notifyAll() 
Communication between threads
Relative to an Object
Example of using:
void todo() {
   synchronized(object){  
      try{
         object.wait();
      } catch(InterruptedException e) {
         System.out.println("Interupted");
      }
   object.notify();
   object.notifyAll();
}
Описание слайда:
wait() notify() notifyAll() Communication between threads Relative to an Object Example of using: void todo() { synchronized(object){ try{ object.wait(); } catch(InterruptedException e) { System.out.println("Interupted"); } object.notify(); object.notifyAll(); }

Слайд 24





Thread summary
Описание слайда:
Thread summary

Слайд 25


Multithreading IO Streams Java Core, слайд №25
Описание слайда:

Слайд 26





Typical threads work:
Goal
to block (wait) the consumer until the basket reaches some fruit
Описание слайда:
Typical threads work: Goal to block (wait) the consumer until the basket reaches some fruit

Слайд 27





Data streams
IO API (Input & Output)  — Java API, designed for streaming.
There are defined input and output streams in java.io (InputStream and OutputStream)
Resource or Destination:
Console
File
Buffer etc.
Описание слайда:
Data streams IO API (Input & Output)  — Java API, designed for streaming. There are defined input and output streams in java.io (InputStream and OutputStream) Resource or Destination: Console File Buffer etc.

Слайд 28





Some classes of Java IO API
InputStream / OutputStream
Reader / Writer
InputStreamReader / OutputStreamWriter
FileInputStream / FileOutputStream
FileReader / FileWriter
BufferedInputStream / BufferedOutputStream
BufferedReader / BufferedWriter
Описание слайда:
Some classes of Java IO API InputStream / OutputStream Reader / Writer InputStreamReader / OutputStreamWriter FileInputStream / FileOutputStream FileReader / FileWriter BufferedInputStream / BufferedOutputStream BufferedReader / BufferedWriter

Слайд 29





Some classes of Java IO API
There are two abstract classes which base all the classes controlling by the streams of bytes:
InputStream (represents input streams)
OutputStream (represents output streams)
To work with the streams of characters there are defined abstract classes:
Reader (for reading streams of characters) 
Writer (for recording streams of symbols).
There are a bridge from byte streams to character streams
InputStreamReader reads bytes and decodes them into characters using a specified charset
OutputStreamWriter writes characters to it are encoded into bytes using a specified charset
Описание слайда:
Some classes of Java IO API There are two abstract classes which base all the classes controlling by the streams of bytes: InputStream (represents input streams) OutputStream (represents output streams) To work with the streams of characters there are defined abstract classes: Reader (for reading streams of characters) Writer (for recording streams of symbols). There are a bridge from byte streams to character streams InputStreamReader reads bytes and decodes them into characters using a specified charset OutputStreamWriter writes characters to it are encoded into bytes using a specified charset

Слайд 30





Java Input and Output Stream
Описание слайда:
Java Input and Output Stream

Слайд 31





File Output
import java.io.*;
public class TestFile {
public static void main(String[] args) {
byte[] w = { 48, 49, 50 };
String fileName = "test.txt";
FileOutputStream outFile;
try {
outFile = new FileOutputStream(fileName);
System.out.println("Output file was opened.");
outFile.write(w);
System.out.println("Saved: " + w.length + " bytes.");
outFile.close();
System.out.println("Output stream was closed.");
} catch (IOException e) {
System.out.println("File Write Error: " + fileName);
}
} }
Описание слайда:
File Output import java.io.*; public class TestFile { public static void main(String[] args) { byte[] w = { 48, 49, 50 }; String fileName = "test.txt"; FileOutputStream outFile; try { outFile = new FileOutputStream(fileName); System.out.println("Output file was opened."); outFile.write(w); System.out.println("Saved: " + w.length + " bytes."); outFile.close(); System.out.println("Output stream was closed."); } catch (IOException e) { System.out.println("File Write Error: " + fileName); } } }

Слайд 32





File Input
import java.io.*;
public class TestFileOutput {
public static void main(String[] args) {
byte[] r = new byte[10];
String fileName = "test.txt";
FileInputStream inFile;
try {
inFile = new FileInputStream(fileName);
System.out.println("Input file was opened.");
int bytesAv = inFile.available();     // Bytes count
System.out.println("Bytes count: " + bytesAv + " Bytes");
int count = inFile.read(r, 0, bytesAv);
System.out.println("Was readed: " + count + " bytes.");
System.out.println(r[0] + " " + r[1] + " " + r[2]);
inFile.close();
System.out.println("Input stream was closed.");
} catch (IOException e) {
System.out.println("File Read/Write Error: " + fileName);
} } }
Описание слайда:
File Input import java.io.*; public class TestFileOutput { public static void main(String[] args) { byte[] r = new byte[10]; String fileName = "test.txt"; FileInputStream inFile; try { inFile = new FileInputStream(fileName); System.out.println("Input file was opened."); int bytesAv = inFile.available(); // Bytes count System.out.println("Bytes count: " + bytesAv + " Bytes"); int count = inFile.read(r, 0, bytesAv); System.out.println("Was readed: " + count + " bytes."); System.out.println(r[0] + " " + r[1] + " " + r[2]); inFile.close(); System.out.println("Input stream was closed."); } catch (IOException e) { System.out.println("File Read/Write Error: " + fileName); } } }

Слайд 33





File Input/Output
import java.io.*;
public class Test2 {
public static void main(String[] args) {
FileInputStream inFile1 = null;
FileInputStream inFile2 = null;
SequenceInputStream sequenceStream = null;
FileOutputStream outFile = null;
try {
inFile1 = new FileInputStream("file1.txt");
inFile2 = new FileInputStream("file2.txt");
sequenceStream = 
    new SequenceInputStream(inFile1, inFile2);
Описание слайда:
File Input/Output import java.io.*; public class Test2 { public static void main(String[] args) { FileInputStream inFile1 = null; FileInputStream inFile2 = null; SequenceInputStream sequenceStream = null; FileOutputStream outFile = null; try { inFile1 = new FileInputStream("file1.txt"); inFile2 = new FileInputStream("file2.txt"); sequenceStream = new SequenceInputStream(inFile1, inFile2);

Слайд 34





File Input/Output
outFile = new FileOutputStream("file4.txt");
int readedByte = sequenceStream.read();
while (readedByte != -1) {
outFile.write(readedByte);
readedByte = sequenceStream.read();
}
} catch (IOException e) {
System.out.println("IOException: " + e.toString());
} finally {
try {
sequenceStream.close();
outFile.close();
} catch (IOException e) { }
}
}
}
Описание слайда:
File Input/Output outFile = new FileOutputStream("file4.txt"); int readedByte = sequenceStream.read(); while (readedByte != -1) { outFile.write(readedByte); readedByte = sequenceStream.read(); } } catch (IOException e) { System.out.println("IOException: " + e.toString()); } finally { try { sequenceStream.close(); outFile.close(); } catch (IOException e) { } } } }

Слайд 35





File Input/Output
Reading from external devices – almost always necessary for buffer to be used
FileReader and FileWriter classes inherited from InputStreamReader and OutputStreamWriter.
The InputStreamReader class is intended to wrap an InputStream, thereby turning the byte based input stream into a character based Reader.
Описание слайда:
File Input/Output Reading from external devices – almost always necessary for buffer to be used FileReader and FileWriter classes inherited from InputStreamReader and OutputStreamWriter. The InputStreamReader class is intended to wrap an InputStream, thereby turning the byte based input stream into a character based Reader.

Слайд 36





File Input/Output
public static void main(String[] args) {
String fileName = "file.txt";
FileWriter fw = null;
BufferedWriter bw = null;
FileReader fr = null;
BufferedReader br = null;
String data = "Some data to be written and readed\n";
try {
fw = new FileWriter(fileName);
bw = new BufferedWriter(fw);
System.out.println("Write data to file: " + fileName);
for (int i = (int) (Math.random() * 10); --i >= 0;) {
bw.write(data);
}
bw.close();
Описание слайда:
File Input/Output public static void main(String[] args) { String fileName = "file.txt"; FileWriter fw = null; BufferedWriter bw = null; FileReader fr = null; BufferedReader br = null; String data = "Some data to be written and readed\n"; try { fw = new FileWriter(fileName); bw = new BufferedWriter(fw); System.out.println("Write data to file: " + fileName); for (int i = (int) (Math.random() * 10); --i >= 0;) { bw.write(data); } bw.close();

Слайд 37





File Input/Output
fr = new FileReader(fileName);
br = new BufferedReader(fr);
String s = null;
int count = 0;
System.out.println("Read data from file: " 
						+ fileName);
while ((s = br.readLine()) != null) {
System.out.println("row " + ++count 
					+ " read:" + s);
}
br.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
Описание слайда:
File Input/Output fr = new FileReader(fileName); br = new BufferedReader(fr); String s = null; int count = 0; System.out.println("Read data from file: " + fileName); while ((s = br.readLine()) != null) { System.out.println("row " + ++count + " read:" + s); } br.close(); } catch (Exception e) { e.printStackTrace(); } } }

Слайд 38





Practical tasks
Output text «I study Java» 10 times with the intervals of one second (Thread.sleep(1000);).
Output two messages «Hello, world» and «Peace in the peace» 5 times each with the intervals of 2 seconds, and the second - 3 seconds. After printing messages, print the text «My name is …»
Prepare mytext.txt file with a lot of text inside.
Read context from file into array of strings.
Each array item contains one line from file.
Complete next tasks:
   1) count and write the number of symbols in every line.
   2) find the longest and the shortest line. 
   3) find and write only that lines, which consist of word «var»
Описание слайда:
Practical tasks Output text «I study Java» 10 times with the intervals of one second (Thread.sleep(1000);). Output two messages «Hello, world» and «Peace in the peace» 5 times each with the intervals of 2 seconds, and the second - 3 seconds. After printing messages, print the text «My name is …» Prepare mytext.txt file with a lot of text inside. Read context from file into array of strings. Each array item contains one line from file. Complete next tasks: 1) count and write the number of symbols in every line. 2) find the longest and the shortest line. 3) find and write only that lines, which consist of word «var»

Слайд 39





HomeWork
Register at http://www.betterprogrammer.com/ 
Install JDK 6 or configure your IDE to use it: http://www.oracle.com/technetwork/java/javase/downloads/java-archive-downloads-javase6-419409.html#jdk-6u45-oth-JPR 
Earn certificate with mark at least 75%
Описание слайда:
HomeWork Register at http://www.betterprogrammer.com/ Install JDK 6 or configure your IDE to use it: http://www.oracle.com/technetwork/java/javase/downloads/java-archive-downloads-javase6-419409.html#jdk-6u45-oth-JPR Earn certificate with mark at least 75%

Слайд 40





Homework
Run three threads and output there different messages for 5 times. The third thread supposed to start after finishing working of the two previous threads.
Cause a deadlock. Organize the expectations of ending a thread in main(), and make the end of the method main() in this thread.
Create a thread «one», which would start the thread «two», which has to output its number («Thread number two») 3 times and create thread «three», which would to output message «Thread number three» 5 times.
Create file1.txt file with a text about your career.
Read context from file into array of strings. Each array item contains one line from file.
Write in to the file2.txt
   1) number of lines in file1.txt.
   2) the longest line in file1.txt.
   3) your name and birthday date.
Описание слайда:
Homework Run three threads and output there different messages for 5 times. The third thread supposed to start after finishing working of the two previous threads. Cause a deadlock. Organize the expectations of ending a thread in main(), and make the end of the method main() in this thread. Create a thread «one», which would start the thread «two», which has to output its number («Thread number two») 3 times and create thread «three», which would to output message «Thread number three» 5 times. Create file1.txt file with a text about your career. Read context from file into array of strings. Each array item contains one line from file. Write in to the file2.txt 1) number of lines in file1.txt. 2) the longest line in file1.txt. 3) your name and birthday date.



Похожие презентации
Mypresentation.ru
Загрузить презентацию