ThreadsDemo: Remade and including comments

This commit is contained in:
2024-02-26 09:32:42 +01:00
parent 8f9cd50556
commit c06d174ee8
10 changed files with 112 additions and 73 deletions

View File

@@ -1,6 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<classpath> <classpath>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-17">
<attributes>
<attribute name="module" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" path="src"/> <classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="output" path="bin"/> <classpathentry kind="output" path="bin"/>
</classpath> </classpath>

View File

@@ -0,0 +1,2 @@
eclipse.preferences.version=1
encoding/<project>=UTF-8

View File

@@ -0,0 +1,14 @@
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.targetPlatform=17
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
org.eclipse.jdt.core.compiler.compliance=17
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
org.eclipse.jdt.core.compiler.debug.localVariable=generate
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=warning
org.eclipse.jdt.core.compiler.release=enabled
org.eclipse.jdt.core.compiler.source=17

View File

@@ -0,0 +1,14 @@
public class ExtendingThreads extends Thread {
private String myString="";
public ExtendingThreads( String initString ) {
myString = initString;
}
public void run() {
System.out.println( "ExtendingThreads (" + myString + ")");
}
}

View File

@@ -0,0 +1,22 @@
public class ImplementingRunnable implements Runnable{
private String myString="";
public ImplementingRunnable( String initString ) {
myString = initString;
}
public void run() {
System.out.println( "ImplementingRunnable (" + myString + ")" );
// The next section of dead code shows how a thread may be set sleeping
if (false) {
try {
Thread.sleep(5000); // mS
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}

View File

@@ -1,26 +0,0 @@
//
// Demo of thread starting a thread
//
public class Level1Interface implements Runnable {
private String MyString;
public Level1Interface(String InitString) {
MyString = InitString;
}
@Override
public void run() {
System.out.println(MyString + " Interface");
ThreadsExtend myThreadA = new ThreadsExtend( MyString + "A" );
myThreadA.start();
try {
myThreadA.join();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(MyString + " Interface...Done");
}
}

View File

@@ -0,0 +1,23 @@
public class ThreadStartingThread implements Runnable{
private String myString="";
public ThreadStartingThread( String initString ) {
myString = initString;
}
@Override
public void run() {
System.out.println( "ThreadStartingThread (" + myString + ")" );
ExtendingThreads myThreadA = new ExtendingThreads( "ThreadStartingThread " + myString );
myThreadA.start();
try {
myThreadA.join();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

View File

@@ -1,23 +1,42 @@
// Karsten Jeppesen, UCN
// There are 3 ways to start threads.
// This demo will show two of the options
// - by extending the Thread class and overriding its run() method
// - by implementing the Runnable interface
//
public class ThreadsDemo { public class ThreadsDemo {
public static void main(String[] args) { public static void main(String[] args) {
ThreadsExtend myThreadA = new ThreadsExtend("A"); System.out.println("Main");
ThreadsExtend myThreadB = new ThreadsExtend("B");
ThreadsExtend myThreadC = new ThreadsExtend("C"); // Extending the Thread class will oc result in a thread
Runnable myRunnableD = new ThreadsInterface("D"); ExtendingThreads myThreadA = new ExtendingThreads("A");
Runnable myRunnableE = new ThreadsInterface("E"); ExtendingThreads myThreadB = new ExtendingThreads("B");
Runnable myRunnableF = new Level1Interface("F"); ExtendingThreads myThreadC = new ExtendingThreads("C");
// implementing the runnable interface will require an additional call to threads
Runnable myRunnableD = new ImplementingRunnable("D");
Runnable myRunnableE = new ImplementingRunnable("E");
Runnable myRunnableF = new ImplementingRunnable("F");
Runnable myRunnableL = new ThreadStartingThread("L");
// Here we inject the runnable into the thread class
Thread myThreadD = new Thread( myRunnableD );
Thread myThreadE = new Thread( myRunnableE );
Thread myThreadF = new Thread( myRunnableF );
Thread myThreadL = new Thread( myRunnableL );
// Starting all threads. Note that sequence can not be guaranteed.
myThreadA.start(); myThreadA.start();
myThreadB.start(); myThreadB.start();
myThreadC.start(); myThreadC.start();
Thread myThreadD = new Thread( myRunnableD );
myThreadD.start(); myThreadD.start();
Thread myThreadE = new Thread( myRunnableE );
myThreadE.start(); myThreadE.start();
Thread myThreadF = new Thread( myRunnableF );
myThreadF.start(); myThreadF.start();
myThreadL.start();
// Waiting for all threads to complete is considered best practice
try { try {
myThreadA.join(); myThreadA.join();
myThreadB.join(); myThreadB.join();
@@ -25,10 +44,12 @@ public class ThreadsDemo {
myThreadD.join(); myThreadD.join();
myThreadE.join(); myThreadE.join();
myThreadF.join(); myThreadF.join();
myThreadL.join();
} catch (InterruptedException e) { } catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace(); e.printStackTrace();
} }
System.out.println("Main Done...");
System.out.println("Main... Done");
} }
} }

View File

@@ -1,20 +0,0 @@
public class ThreadsExtend extends Thread {
private String MyString;
public ThreadsExtend ( String InitString ) {
MyString = InitString;
}
public void run() {
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(MyString + " running");
}
}

View File

@@ -1,15 +0,0 @@
public class ThreadsInterface implements Runnable {
private String MyString;
public ThreadsInterface(String InitString) {
MyString = InitString;
}
@Override
public void run() {
System.out.println(MyString + " Interface");
}
}