2
0
Fork 0
mirror of https://github.com/MartinThoma/LaTeX-examples.git synced 2025-04-25 06:18:05 +02:00

Java hinzugefügt

This commit is contained in:
Martin Thoma 2014-03-24 17:04:40 +01:00
parent 80e8df59d6
commit b7371b1ac2
5 changed files with 220 additions and 0 deletions

View file

@ -0,0 +1,72 @@
%!TEX root = Programmierparadigmen.tex
\chapter{Java}
\index{Java|(}
Im Folgenden wird in aller Kürze erklärt, wie man in Java Programme schreibt,
die auf mehreren Prozessoren laufen.
\section{Thread, ThreadPool und Runnable}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\texttt{Interface Runnable}\\
\-\hspace{0.8cm}$\leftharpoonup$ \texttt{java.lang.Thread}\xindex{Runnable}%
\begin{itemize}
\item Methods:
\begin{itemize}
\item \texttt{void run()}: When an object implementing interface
Runnable is used to create a thread, starting the thread causes the
object's run method to be called in that separately executing thread.
\end{itemize}
\end{itemize}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\texttt{Class Thread}\\
\-\hspace{0.8cm}$\leftharpoonup$ \texttt{java.lang.Thread}\xindex{Thread}%
\begin{itemize}
\item implements Runnable
\end{itemize}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\texttt{Class ThreadPoolExecutor}\xindex{ThreadPoolExecutor}\\
\-\hspace{0.8cm}$\leftharpoonup$ \texttt{java.util.concurrent.ThreadPoolExecutor}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\texttt{Interface Callable<V>}\xindex{Callable}\\
\-\hspace{0.8cm}$\leftharpoonup$ \texttt{java.util.concurrent}
\begin{itemize}
\item Parameter: \texttt{V} - the result type of method call
\end{itemize}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{Futures}\xindex{Future}\index{Promise|see{Future}}
\enquote{Ein Future (engl. \enquote{Zukunft}) oder ein Promise (engl. \enquote{Versprechen}) bezeichnet in der Programmierung einen Platzhalter (Proxy) für ein Ergebnis, das noch nicht bekannt ist, meist weil seine Berechnung noch nicht abgeschlossen ist.}
\texttt{Interface Future<V>}\xindex{Future}\\
\-\hspace{0.8cm}$\leftharpoonup$ \texttt{java.util.concurrent}
\textbf{Parameter}:
\begin{itemize}
\item \texttt{V}: The result type returned by this Future's get method
\end{itemize}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\textbf{Beispiel}:
% Zu lang, geht das kürzer?
%\inputminted[numbersep=5pt, tabsize=4]{java}{scripts/java/matrix-multiplication.java}
\begin{beispiel}[Runnable, ExecutorService, ThreadPool\footnotemark]
\inputminted[numbersep=5pt, tabsize=4]{java}{scripts/java/vorlesung-futures-basics.java}
\end{beispiel}
\footnotetext{WS 2013/2014, Kapitel 41, Folie 26}
\section{Literatur}
\begin{itemize}
\item \href{http://openbook.galileocomputing.de/javainsel9/javainsel_14_004.htm}{Java ist auch eine Insel}: Kapitel 14 -
Threads und nebenläufige Programmierung
\item \href{http://www.vogella.com/tutorials/JavaConcurrency/article.html}{vogella.com}: Java concurrency (multi-threading) - Tutorial
\item Links zur offiziellen Java 8 Dokumentation:
\begin{itemize}
\item \href{http://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ThreadPoolExecutor.html}{ThreadPoolExecutor}
\item \href{http://docs.oracle.com/javase/8/docs/api/java/lang/Runnable.html}{Runnable}
\item \href{http://docs.oracle.com/javase/8/docs/api/java/lang/Thread.html}{Thread}
\item \href{http://docs.oracle.com/javase/8/docs/api/java/util/concurrent/Callable.html}{Callable}
\item \href{http://docs.oracle.com/javase/8/docs/api/java/util/concurrent/Future.html}{Future}
\end{itemize}
\end{itemize}
\index{Java|)}

View file

@ -117,6 +117,7 @@
\input{Lambda}
\input{Typinferenz}
\input{Parallelitaet}
\input{Java}
\input{Haskell}
\input{Prolog}
\input{Scala}

View file

@ -0,0 +1,130 @@
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.LinkedList;
import java.util.List;
import java.util.ArrayList;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
public class Shell {
static List<ArrayList<ArrayList<Integer>>> read(String filename) {
ArrayList<ArrayList<Integer>> A = new ArrayList<ArrayList<Integer>>();
ArrayList<ArrayList<Integer>> B = new ArrayList<ArrayList<Integer>>();
String thisLine;
try {
BufferedReader br = new BufferedReader(new FileReader(filename));
// Begin reading A
while ((thisLine = br.readLine()) != null) {
if (thisLine.trim().equals("")) {
break;
} else {
ArrayList<Integer> line = new ArrayList<Integer>();
String[] lineArray = thisLine.split("\t");
for (String number : lineArray) {
line.add(Integer.parseInt(number));
}
A.add(line);
}
}
// Begin reading B
while ((thisLine = br.readLine()) != null) {
ArrayList<Integer> line = new ArrayList<Integer>();
String[] lineArray = thisLine.split("\t");
for (String number : lineArray) {
line.add(Integer.parseInt(number));
}
B.add(line);
}
br.close();
} catch (IOException e) {
System.err.println("Error: " + e);
}
List<ArrayList<ArrayList<Integer>>> res = new LinkedList<ArrayList<ArrayList<Integer>>>();
res.add(A);
res.add(B);
return res;
}
static void printMatrix(int[][] matrix) {
for (int[] line : matrix) {
int i = 0;
StringBuilder sb = new StringBuilder(matrix.length);
for (int number : line) {
if (i != 0) {
sb.append("\t");
} else {
i++;
}
sb.append(number);
}
System.out.println(sb.toString());
}
}
public static int[][] parallelMult(ArrayList<ArrayList<Integer>> A,
ArrayList<ArrayList<Integer>> B, int threadNumber) {
int[][] C = new int[A.size()][B.get(0).size()];
ExecutorService executor = Executors.newFixedThreadPool(threadNumber);
List<Future<int[][]>> list = new ArrayList<Future<int[][]>>();
int part = A.size() / threadNumber;
if (part < 1) {
part = 1;
}
for (int i = 0; i < A.size(); i += part) {
System.err.println(i);
Callable<int[][]> worker = new LineMultiplier(A, B, i, i+part);
Future<int[][]> submit = executor.submit(worker);
list.add(submit);
}
// now retrieve the result
int start = 0;
int CF[][];
for (Future<int[][]> future : list) {
try {
CF = future.get();
for (int i=start; i < start+part; i += 1) {
C[i] = CF[i];
}
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
start+=part;
}
executor.shutdown();
return C;
}
public static void main(String[] args) {
String filename;
int cores = Runtime.getRuntime().availableProcessors();
System.err.println("Number of cores:\t" + cores);
int threads;
if (args.length < 3) {
filename = "3.in";
threads = cores;
} else {
filename = args[1];
threads = Integer.parseInt(args[2]);
}
List<ArrayList<ArrayList<Integer>>> matrices = read(filename);
ArrayList<ArrayList<Integer>> A = matrices.get(0);
ArrayList<ArrayList<Integer>> B = matrices.get(1);
int[][] C = parallelMult(A, B, threads);
printMatrix(C);
}
}

View file

@ -0,0 +1,17 @@
public final class StringTask implements Runnable {
int id;
public StringTask(int id) {
this.id = id;
}
public void run() {
// do calculation
}
}
ExecutorService pool =
Executors.newFixedThreadPool(4);
for(int i = 0; i < 10; i++){
pool.execute(new StringTask(i));
}
pool.shutdown();
executor.awaitTermination();