0% found this document useful (0 votes)
8 views2 pages

Java Multithreading Basics Guide

Uploaded by

Bo Stevens
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views2 pages

Java Multithreading Basics Guide

Uploaded by

Bo Stevens
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Serialization: The Java Way

Assignment Description
We’re going to dip our toes into the water with threading in this assignment. We won’t be doing anything terribly intensive, but we
will be creating an application that is threaded, just to learn how the process works

Objectives
1. Objective 1

Objective 1

1 Building our Threaded class and driver Class


Our goal here is pretty simple, to build our threaded class, and the driver class where we’ll be creating our threads from.

Checklist
• Create a Java file called [Link]
o With a class called UselessThreader
• Create a class called ThreadedClass inside [Link]
• Make it extend Thread or implement Runnable
• Give it a variable called Name

1.1 Creating ThreadedClass


We’re skipping over how to create the UselessThreader file because that should hopefully be pretty trivial by this point. What might be
interesting to learn however, is that Java supports the ability to create multiple classes within a single file
public class UselessThreader{
public static void main(String[] args) {

}
}
class ThreadedClass{

}
-The creation of a second class inside UselessThr [Link]

Now, we have two options for making a class multithreaded, extending Thread or implementing Runnable. The easier of the two is to
extend Thread, so let’s do that. When we do, we also have to add a run() method to the class (it will be used later as the thread’s
entrypoint).
class ThreadedClass extends Thread{
public void run() {

}
}

Now do a couple more less interesting things to ThreadedClass


• Add an instance variable called Name of type String
• Create a constructor that takes a String as an argument
o And sets Name equal to the String we passed in
• Add a print statement to our run() method
o And have it print out “Hi there! My name is “ + Name

1.2 Creating and Running our Threads


Now that we added the ability to push instances of ThreadedClass out to their own thread, let’s go ahead and create some in our main
method and then run them.

public static void main(String[] args) {


ThreadedClass threadOne = new ThreadedClass("one");
ThreadedClass threadTwo = new ThreadedClass("two");
ThreadedClass threadThree = new ThreadedClass("three");
[Link]();
[Link]();
[Link]();
}
1.3 Testing
If we go ahead and run our program now, we should be able to see each of our threads printing out their “hello” message, as well as their name.

Note how the print statements are out of order in this example. If you run your code enough, you should notice that the order the print
statements show up in the console can change. This happens for a couple reasons.

When we create our threads, they get sent to the operating system. The operating system then decides, at its own discretion, when a task
actually gets to be run on the CPU. This means that threadTwo can actually start running well after threadThree has already begun.
When data gets split up between multiple CPU cores, even if both threads enter their respective cores at the same time, one of them might still
finish nano-seconds earlier than another. This behavior is due to a number of factors that we won’t go into depth on, but what’s important is
that as far as the user (programmer) is concerned, it can be a complete coin-toss as to which thread will finish first.

This is very important as it means our code goes from completely deterministic (the results will be the same every time we run the code) to
seemingly (but not actually*) non-deterministic (the results are different each time we run it). This is a major concern from program health and
safety, and will be the focus of future assignments.

So why the * earlier?


Determinism is a very important topic in computing (the i dea that our code will always execute the same way every time). I ’m basically
trying to avoid a massive argument by hand -waving. For interesting reading, try looking up whether or not the human brain is
deterministic. You’ll see why this is such a heated topic pretty quickly.

You might also like