Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 83b3122

Browse filesBrowse files
committed
new commit
1 parent 0e8f3fa commit 83b3122
Copy full SHA for 83b3122

File tree

Expand file treeCollapse file tree

36 files changed

+3424
-0
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

36 files changed

+3424
-0
lines changed
Open diff view settings
Collapse file

‎JavaMultiThreadingCodes/build.xml‎

Copy file name to clipboard
+73Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!-- You may freely edit this file. See commented blocks below for -->
3+
<!-- some examples of how to customize the build. -->
4+
<!-- (If you delete it and reopen the project it will be recreated.) -->
5+
<!-- By default, only the Clean and Build commands use this build script. -->
6+
<!-- Commands such as Run, Debug, and Test only use this build script if -->
7+
<!-- the Compile on Save feature is turned off for the project. -->
8+
<!-- You can turn off the Compile on Save (or Deploy on Save) setting -->
9+
<!-- in the project's Project Properties dialog box.-->
10+
<project name="JavaMultiThreadingCodes" default="default" basedir=".">
11+
<description>Builds, tests, and runs the project JavaMultiThreadingCodes.</description>
12+
<import file="nbproject/build-impl.xml"/>
13+
<!--
14+
15+
There exist several targets which are by default empty and which can be
16+
used for execution of your tasks. These targets are usually executed
17+
before and after some main targets. They are:
18+
19+
-pre-init: called before initialization of project properties
20+
-post-init: called after initialization of project properties
21+
-pre-compile: called before javac compilation
22+
-post-compile: called after javac compilation
23+
-pre-compile-single: called before javac compilation of single file
24+
-post-compile-single: called after javac compilation of single file
25+
-pre-compile-test: called before javac compilation of JUnit tests
26+
-post-compile-test: called after javac compilation of JUnit tests
27+
-pre-compile-test-single: called before javac compilation of single JUnit test
28+
-post-compile-test-single: called after javac compilation of single JUunit test
29+
-pre-jar: called before JAR building
30+
-post-jar: called after JAR building
31+
-post-clean: called after cleaning build products
32+
33+
(Targets beginning with '-' are not intended to be called on their own.)
34+
35+
Example of inserting an obfuscator after compilation could look like this:
36+
37+
<target name="-post-compile">
38+
<obfuscate>
39+
<fileset dir="${build.classes.dir}"/>
40+
</obfuscate>
41+
</target>
42+
43+
For list of available properties check the imported
44+
nbproject/build-impl.xml file.
45+
46+
47+
Another way to customize the build is by overriding existing main targets.
48+
The targets of interest are:
49+
50+
-init-macrodef-javac: defines macro for javac compilation
51+
-init-macrodef-junit: defines macro for junit execution
52+
-init-macrodef-debug: defines macro for class debugging
53+
-init-macrodef-java: defines macro for class execution
54+
-do-jar: JAR building
55+
run: execution of project
56+
-javadoc-build: Javadoc generation
57+
test-report: JUnit report generation
58+
59+
An example of overriding the target for project execution could look like this:
60+
61+
<target name="run" depends="JavaMultiThreadingCodes-impl.jar">
62+
<exec dir="bin" executable="launcher.exe">
63+
<arg file="${dist.jar}"/>
64+
</exec>
65+
</target>
66+
67+
Notice that the overridden target depends on the jar target and not only on
68+
the compile target as the regular run target does. Again, for a list of available
69+
properties which you can use, check the target you are overriding in the
70+
nbproject/build-impl.xml file.
71+
72+
-->
73+
</project>
Collapse file
+3Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Manifest-Version: 1.0
2+
X-COMMENT: Main-Class will be added automatically by build
3+
Collapse file

‎JavaMultiThreadingCodes/nbproject/build-impl.xml‎

Copy file name to clipboardExpand all lines: JavaMultiThreadingCodes/nbproject/build-impl.xml
+1,413Lines changed: 1413 additions & 0 deletions
Large diffs are not rendered by default.
Collapse file
+8Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
build.xml.data.CRC32=4d3d93c3
2+
build.xml.script.CRC32=a6a26692
3+
build.xml.stylesheet.CRC32=8064a381@1.74.2.48
4+
# This file is used by a NetBeans-based IDE to track changes in generated files such as build-impl.xml.
5+
# Do not edit this file. You may delete it but then the IDE will never regenerate such files for you.
6+
nbproject/build-impl.xml.data.CRC32=4d3d93c3
7+
nbproject/build-impl.xml.script.CRC32=fa6ba560
8+
nbproject/build-impl.xml.stylesheet.CRC32=876e7a8f@1.74.2.48
Collapse file
+73Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
annotation.processing.enabled=true
2+
annotation.processing.enabled.in.editor=false
3+
annotation.processing.processor.options=
4+
annotation.processing.processors.list=
5+
annotation.processing.run.all.processors=true
6+
annotation.processing.source.output=${build.generated.sources.dir}/ap-source-output
7+
build.classes.dir=${build.dir}/classes
8+
build.classes.excludes=**/*.java,**/*.form
9+
# This directory is removed when the project is cleaned:
10+
build.dir=build
11+
build.generated.dir=${build.dir}/generated
12+
build.generated.sources.dir=${build.dir}/generated-sources
13+
# Only compile against the classpath explicitly listed here:
14+
build.sysclasspath=ignore
15+
build.test.classes.dir=${build.dir}/test/classes
16+
build.test.results.dir=${build.dir}/test/results
17+
# Uncomment to specify the preferred debugger connection transport:
18+
#debug.transport=dt_socket
19+
debug.classpath=\
20+
${run.classpath}
21+
debug.test.classpath=\
22+
${run.test.classpath}
23+
# Files in build.classes.dir which should be excluded from distribution jar
24+
dist.archive.excludes=
25+
# This directory is removed when the project is cleaned:
26+
dist.dir=dist
27+
dist.jar=${dist.dir}/JavaMultiThreadingCodes.jar
28+
dist.javadoc.dir=${dist.dir}/javadoc
29+
excludes=
30+
includes=**
31+
jar.compress=false
32+
javac.classpath=
33+
# Space-separated list of extra javac options
34+
javac.compilerargs=
35+
javac.deprecation=false
36+
javac.processorpath=\
37+
${javac.classpath}
38+
javac.source=1.7
39+
javac.target=1.7
40+
javac.test.classpath=\
41+
${javac.classpath}:\
42+
${build.classes.dir}
43+
javac.test.processorpath=\
44+
${javac.test.classpath}
45+
javadoc.additionalparam=
46+
javadoc.author=false
47+
javadoc.encoding=${source.encoding}
48+
javadoc.noindex=false
49+
javadoc.nonavbar=false
50+
javadoc.notree=false
51+
javadoc.private=false
52+
javadoc.splitindex=true
53+
javadoc.use=true
54+
javadoc.version=false
55+
javadoc.windowtitle=
56+
main.class=javaapplication7.JavaApplication7
57+
manifest.file=manifest.mf
58+
meta.inf.dir=${src.dir}/META-INF
59+
mkdist.disabled=false
60+
platform.active=default_platform
61+
run.classpath=\
62+
${javac.classpath}:\
63+
${build.classes.dir}
64+
# Space-separated list of JVM arguments used when running the project.
65+
# You may also define separate properties like run-sys-prop.name=value instead of -Dname=value.
66+
# To set system properties for unit tests define test-sys-prop.name=value:
67+
run.jvmargs=
68+
run.test.classpath=\
69+
${javac.test.classpath}:\
70+
${build.test.classes.dir}
71+
source.encoding=UTF-8
72+
src.dir=src
73+
test.src.dir=test
Collapse file
+15Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://www.netbeans.org/ns/project/1">
3+
<type>org.netbeans.modules.java.j2seproject</type>
4+
<configuration>
5+
<data xmlns="http://www.netbeans.org/ns/j2se-project/3">
6+
<name>JavaMultiThreadingCodes</name>
7+
<source-roots>
8+
<root id="src.dir"/>
9+
</source-roots>
10+
<test-roots>
11+
<root id="test.src.dir"/>
12+
</test-roots>
13+
</data>
14+
</configuration>
15+
</project>
Collapse file
+69Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package CallableAndFuture_13;
2+
3+
/**
4+
* Callable and Future in Java to get results from your threads and to allow
5+
* your threads to throw exceptions. Plus, Future allows you to control your
6+
* threads, checking to see if they’re running or not, waiting for results and
7+
* even interrupting them or descheduling them.
8+
*
9+
* Runnable is default abstraction for creating a task in Java. It has a single
10+
* method run() that accepts no arguments and returns no value, nor it can throw
11+
* any checked exception. To overcome these limitations, Java 5 introduced a new
12+
* task abstraction through Callable interface.
13+
*
14+
* Codes with minor comments are from http://www.caveofprogramming.com/youtube/
15+
* also freely available at
16+
* https://www.udemy.com/java-multithreading/?couponCode=FREE
17+
*
18+
* @author Z.B. Celik <celik.berkay@gmail.com>
19+
*/
20+
import java.io.IOException;
21+
import java.util.Random;
22+
import java.util.concurrent.Callable;
23+
import java.util.concurrent.ExecutionException;
24+
import java.util.concurrent.ExecutorService;
25+
import java.util.concurrent.Executors;
26+
import java.util.concurrent.Future;
27+
import java.util.concurrent.TimeUnit;
28+
29+
public class App {
30+
31+
public static void main(String[] args) throws InterruptedException {
32+
ExecutorService executor = Executors.newCachedThreadPool();
33+
//anonymous call of Callable
34+
Future<Integer> future = executor.submit(new Callable<Integer>() {
35+
36+
@Override
37+
//return value is Integer
38+
public Integer call() throws Exception {
39+
Random random = new Random();
40+
int duration = random.nextInt(4000);
41+
if (duration > 2000) {
42+
throw new IOException("Sleeping for too long.");
43+
}
44+
System.out.println("Starting ...");
45+
46+
try {
47+
Thread.sleep(duration);
48+
} catch (InterruptedException e) {
49+
e.printStackTrace();
50+
}
51+
System.out.println("Finished.");
52+
return duration;
53+
}
54+
});
55+
56+
executor.shutdown();
57+
executor.awaitTermination(1, TimeUnit.DAYS);
58+
try {
59+
//get returned value from call()
60+
System.out.println("Result is: " + future.get());
61+
} catch (InterruptedException e) {
62+
e.printStackTrace();
63+
} catch (ExecutionException e) {
64+
IOException ex = (IOException) e.getCause();
65+
System.out.println(ex.getMessage());
66+
}
67+
}
68+
69+
}
Collapse file
+60Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package CallableAndFuture_13;
2+
3+
/**
4+
* Understanding Callable
5+
*
6+
* @author Z.B. Celik <celik.berkay@gmail.com>
7+
*/
8+
import java.util.ArrayList;
9+
import java.util.concurrent.Callable;
10+
import java.util.concurrent.ExecutionException;
11+
import java.util.concurrent.ExecutorService;
12+
import java.util.concurrent.Executors;
13+
import java.util.concurrent.Future;
14+
import java.util.concurrent.TimeUnit;
15+
16+
class MyCallable implements Callable<Integer> {
17+
18+
Integer value;
19+
20+
public MyCallable(Integer i) {
21+
this.value = i;
22+
}
23+
24+
@Override
25+
public Integer call() throws Exception {
26+
Integer sum = 0;
27+
for (int i = 0; i < value; i++) {
28+
sum += i;
29+
}
30+
System.out.println("Sum in Callable.Call() " + sum);
31+
return sum;
32+
}
33+
34+
}
35+
36+
public class App2 {
37+
38+
public static void main(String[] args) throws InterruptedException {
39+
ArrayList<Integer> list = new ArrayList<>();
40+
ExecutorService executor = Executors.newCachedThreadPool();
41+
Future<Integer> future = null;
42+
Callable<Integer> callable = null;
43+
for (int i = 1; i < 10; i++) {
44+
future = executor.submit(new MyCallable(i));
45+
try {
46+
list.add(future.get());
47+
} catch (ExecutionException ex) {
48+
System.out.println(ex.getMessage());
49+
}
50+
}
51+
52+
executor.shutdown();
53+
executor.awaitTermination(1, TimeUnit.DAYS);
54+
for (int i = 0; i < list.size(); i++) {
55+
//get returned values from call()
56+
System.out.println("List Values " + i + " Value: " + list.get(i));
57+
58+
}
59+
}
60+
}
Collapse file
+77Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package CallableAndFuture_13;
2+
3+
import java.util.concurrent.Callable;
4+
import java.util.concurrent.ExecutorService;
5+
import java.util.concurrent.Future;
6+
import java.util.concurrent.ScheduledThreadPoolExecutor;
7+
8+
/**
9+
* Source:http://java-x.blogspot.com.tr/2006/11/java-5-concurrency-callable-and-future.html
10+
* Till Java 1.4, threads could be implemented by either implementing Runnable
11+
* or extending Thread. This was quite simple, but had a serious limitation -
12+
* They have a run method that cannot return values. In order to side-step this,
13+
* most programmers use side-effects (writing to a file etc.) to mimic returning
14+
* values to the invoker of the thread. Java 5 introduces the Callable
15+
* interface, that allows users to return values from a thread
16+
*
17+
* Runnable vs Callable<T>
18+
* Runnable Introduced in Java 1.0 Callable<T> Introduced in Java 1.5 as part of
19+
* java.util.concurrent library
20+
*
21+
* Runnable cannot be parameterized Callable is a parameterized type whose type
22+
* parameter indicates the return type of its run method Classes implementing
23+
*
24+
* Runnable needs to implement run() method, classes implementing Callable needs
25+
* to implement call() method
26+
*
27+
* Runnable.run() returns no value, Callable.call() returns a value of Type T
28+
*
29+
* Runnable can not throw checked exceptions, Callable can throw checked
30+
* exceptions
31+
*
32+
* @author Z.B. Celik <celik.berkay@gmail.com>
33+
*/
34+
class CallableImpl implements Callable<Integer> {
35+
36+
private int myName;
37+
38+
CallableImpl(int i) {
39+
myName = i;
40+
}
41+
42+
@Override
43+
public Integer call() {
44+
for (int i = 0; i < 10; i++) {
45+
System.out.println("Thread : " + getMyName() + " value is : " + i);
46+
}
47+
return getMyName();
48+
}
49+
50+
public int getMyName() {
51+
return myName;
52+
}
53+
54+
public void setMyName(int myName) {
55+
this.myName = myName;
56+
}
57+
}
58+
59+
public class CallableTester {
60+
61+
public static void main(String[] args) {
62+
63+
Callable<Integer> callable = new CallableImpl(2);
64+
ExecutorService executor = new ScheduledThreadPoolExecutor(1);
65+
Future<Integer> future = executor.submit(callable);
66+
67+
try {
68+
System.out.println("Future value: " + future.get());
69+
} catch (Exception e) {
70+
e.printStackTrace();
71+
} finally {
72+
executor.shutdown();
73+
executor.isTerminated();
74+
}
75+
}
76+
77+
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.