OOP&M - theory lectures 1
OOP&M – and there it was an eighth day
“If you know what I know, what am I doing here?”- Aristoteles -
OOP&M - theory lectures 2
OOP&M – Threads
Some theory
•Here we are going first to introduce a couple of basic concepts: mono- and multi- processor and tasking computer
•These two sets of prefix and roots generate a final set of four possible combinations of computer architectures that need to be explained in order to understand what a thread is:
•monoprocessor•multiprocessor•monotasking•multitasking
OOP&M - theory lectures 3
OOP&M – Threads
Monoprocessor
•The typical desktop/laptop/handheld computer that we are used to work with has got only one CPU
CPU: Central Process Unit or PROCESSOR
•These processors can execute one or more sequences of code at a time. Each one of the operations that the processor does for the operating system in our computer is called a process
Process: Set of operations done in a computer for accomplishing some goal
OOP&M - theory lectures 4
OOP&M – Threads
Multiprocessor
•Computers with more than one CPU are called multiprocessor computers. If we have structures of several computers connected together but with only one user interface, we will consider it as a multiprocessor architecture
•This computers can execute one or more processes at a time: different processors can attack the different processes
OOP&M - theory lectures 5
OOP&M – Threads
Monotasking
•A TASK is nothing but a sequence of processes that should be executed by the computer
•TASKs are e.g. the execution of a program like the Antivirus-software, a MSOffice session, a window with a game ...
•A monotasking computer is that one that is able of executing only one of those TASKs at a time on the operating system ... this means that the rest of the TASK will be frozen until the TASK under execution finishes its work
OOP&M - theory lectures 6
OOP&M – Threads
Multitasking
•A multitasking computer is that one that is able of executing more than one TASKs at a time on the operating system ...
•The policy of the use of the resources by each TASK is defined by the O.S. E.g. if you open RealPlayer it blocks the use of the soundcard to other programs: we say that RealPlayer allocates the computer´s resources BUT NOT THE PROCESSOR
•Modern O.S. like the latest versions of windows implement multitasking operation in our computers
OOP&M - theory lectures 7
OOP&M – Threads
Examples of operation
•Monoprocessor Monotasking Computer
•Monoprocessor Multitasking Computer
•Multiprocessor Monotasking Computer
•Multiprocessor Multitasking Computer (different cases)
CPU TIME
TASK TIME
CPU TIME
TASK 1 TASK 2
CPU 1
TASK TIME
CPU 2
CPU 1 CPU 2 CPU 1 CPU 2
TASK 1 TASK 2 TASK 1 T2 TASK 2T1
OOP&M - theory lectures 8
OOP&M – Threads
Simultaneity and Concurrency
•There are another couple of concepts that we need to understand before we can enter to the theory of Threads
•Simultaneity: literally consists in carrying out instructions from different codes (or programs) at exactly the same time
•Concurrency: the execution of two or more processes overlapped in time
•Both definitions look almost the same, but the next slide will demonstrate that it is not like that
OOP&M - theory lectures 9
OOP&M – Threads
Simultaneity and Concurrency (2)
•If you analyze the definition of Simultaneity you will find a hole in the relationship of it to our reality: in order to execute two sets of instructions exactly at the same time you need two processors working with both input of data in parallel
•The Java Virtual Machine emulates this mode of working when we are working in a desktop computer by parting the code of each one of the tasks into smaller parts and then executing some parts of one task, afterwards some commands from another task and so on
•This way of working is what we call Concurrency: the emulation of simultaneity
OOP&M - theory lectures 10
OOP&M – Threads
Simultaneity and Concurrency (3)
•If we have multiprocessor architecture-based computers, the Java Virtual Machine will be responsible for the way in how the tasks will be parted and executed
•The execution of Java programs is depending on the architecture of the system where we are working
•With more processors our concurrency will be more and more close to simultaneity
OOP&M - theory lectures 11
OOP&M – Threads
Thread - a definition
•A THREAD is the process of carrying out a set of instructions one at a time. In a more practical way it could look like:
INSTRUCTION 1 :: String winner = “Lisa”;INSTRUCTION 2 :: System.out.println(“And the winner is ” + winner);INSTRUCTION 3 :: String loser = “Bjorn”; INSTRUCTION 4 :: System.out.println(“But ” + loser + “ loses this time”);
•So far we have been doing programs that just look like that. They are composed by only one sequence of instructions that are executed sequentially: they are composed by only one THREAD
time
OOP&M - theory lectures 12
OOP&M – Threads
Thread - a definition (2)
•Some programs need more than only one set of lines of code being executed at a time ... image, e.g. the window of an internet browser:
Scrollbar is controlled by a thread
Independently if the browser is downloading a site, if we write a new address here, the process restart --> there is another thread controlling this
Who controls the buttons of the window? A thread!
THERE ARE MORETHERE ARE MORE
OOP&M - theory lectures 13
OOP&M – Threads
Thread - a definition (3)
•In the example, there are many processes working at the same time in the browser window:
•the URL line, it is waiting for the user to type a new address to look for
•the links on the window, which are active and can be called even if the browser is looking for information (only in some browsers)
•the controls of the window: scrollbar, menus, buttons ...
•Each one of them is controlled by a different thread
OOP&M - theory lectures 14
OOP&M – Threads
Thread - a definition (4)
•How can we compare the threads to the definitions that we saw at the beginning of the lecture (mono- multi- processor/tasking)?
•The Java Virtual Machine can be installed almost on every architecture and running on many different operating systems
•This makes threads NOT comparable to the structures that we saw before. This means that threads look almost like tasks, but they are not the same ... One task could be composed of several threads (one program has got different processes running at a time). Even if we cannot compare both concepts directly, threads and alike from a conceptual point of view
OOP&M - theory lectures 15
OOP&M – Threads
Thread - the class
•Java provides a class, called Thread, that models threads and allows us to build applications and applets in which more than only one of those work
•Using this class, we can create Thread objects that are associated to a particular piece of code that we want to execute simultaneously with other threads
•In the browser example, we would have a thread that executed the code for opening a connection and download a new site to show in parallel to window controls, buttons, a.s.o.
OOP&M - theory lectures 16
OOP&M – Threads
Thread - the class (2)
•The availability of Threads considerably expands the opportunities available to the programmer. Without them, the program just carries one task at the time
•The Thread class provides several methods that allow Thread objects to be controlled:
•*.start() - starts the execution of the Thread. The Thread can exist from before, but it won´t do anything until this method is invoked
•*.suspend() - ask the Thread to suspend the execution until *.resume() is called
•*.resume() - continues the execution of a suspended Thread•*.stop() - asks the Thread to permanently cease the
execution of its code. Once stopped, is cannot be restarted again!!
OOP&M - theory lectures 17
OOP&M – Threads
Thread - the class (3)
•The declaration of a Thread passes by the creation of a new class that extends the Thread library of the Java package:
class myOwnThread extends Thread {rest of the definition
}
• It then requires a method called run, which is the equivalent to the main method in a normal class
public void run()
OOP&M - theory lectures 18
OOP&M – Threads
Thread - the class (4)
•An example:
class myOwnThread extends Thread {public myOwnThread() {}
public void run() {int i = 0;while (i<10) {
System.out.println(“repetition number ”+ i);i++;
}}
}
•What is this code doing?
OOP&M - theory lectures 19
OOP&M – Threads
Thread - the class (5)
•The code was NOT doing anything as far as nobody is calling to it, we need a program that calls to it:
class myThreadCaller {public static void main() {
myOwnThread t1 = new myOwnThread();t1.start();
}}
•You can say that this code is not doing anything special, but there is a big difference if you compare it to other programs, and it is that it can be executed simultaneously with other code
OOP&M - theory lectures 20
OOP&M – Threads
Thread - the class (6)
•The result looks like:
repetition number 0repetition number 1repetition number 2repetition number 3repetition number 4repetition number 5repetition number 6repetition number 7repetition number 8repetition number 9
OOP&M - theory lectures 21
OOP&M – Threads
Thread - the class (7)
•Another example:
class myOwnThread extends Thread {public myOwnThread(String ID) {
this.ID = ID;}
public void run() {int i = 0;while (i<10) {
System.out.println(ID +“ rep. number ”+ i);i++;
}}
private String ID;}
OOP&M - theory lectures 22
OOP&M – Threads
Thread - the class (8)
•Now we can call two different Threads and we can give different names to each one of those, just to see the results:
class myThreadCaller {public static void main() {
myOwnThread t1 = new myOwnThread(“A”);myOwnThread t2 = new myOwnThread(“B”);t1.start();t2.start();
}}
OOP&M - theory lectures 23
OOP&M – Threads
Thread - the class (9)
•The most probable result looks like (depends on the JVM):
A repetition number 0A repetition number 1A repetition number 2A repetition number 3A repetition number 4A repetition number 5A repetition number 6A repetition number 7A repetition number 8A repetition number 9B repetition number 0B repetition number 1B repetition number 2...B repetition number 8B repetition number 9
OOP&M - theory lectures 24
OOP&M – Threads
Thread - the class (10)
•In order to see how the JVM separates the processes we have to introduce the factor time:
class myOwnThread extends Thread {public myOwnThread(String ID) {
this.ID = ID;}public void run() {
int i = 0;while (i<10) {
System.out.println(ID +“repetition number ”+ i);
i++;try { sleep(1); } catch (Exception e) {}
}}private String ID;
}
OOP&M - theory lectures 25
OOP&M – Threads
Thread - the class (11)
•Now we can call two different Threads and we can give different names to each one of those, just to see the results:
class myThreadCaller {public static void main() {
myOwnThread t1 = new myOwnThread(“A”);myOwnThread t2 = new myOwnThread(“B”);t1.start();t2.start();
}}
OOP&M - theory lectures 26
OOP&M – Threads
Thread - the class (12)
•The most probable result looks like (depends on the JVM):
A repetition number 0B repetition number 0A repetition number 1B repetition number 1A repetition number 2B repetition number 2A repetition number 3B repetition number 3A repetition number 4...A repetition number 8B repetition number 8A repetition number 9B repetition number 9