0% found this document useful (0 votes)
14 views26 pages

Thread in TCP

This document discusses threads in distributed programming. It defines a thread as a thread of execution that allows a program to split itself into simultaneous tasks. Java was the first language to support threading. Threads have a lifecycle that includes states like new, runnable, waiting, timed waiting, and terminated. Threads can have different priorities that help determine execution order, though priority does not guarantee order. Distributed programs use multiple threads to handle multiple requests simultaneously.

Uploaded by

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

Thread in TCP

This document discusses threads in distributed programming. It defines a thread as a thread of execution that allows a program to split itself into simultaneous tasks. Java was the first language to support threading. Threads have a lifecycle that includes states like new, runnable, waiting, timed waiting, and terminated. Threads can have different priorities that help determine execution order, though priority does not guarantee order. Distributed programs use multiple threads to handle multiple requests simultaneously.

Uploaded by

tikowanaasu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 26

Distributed Programming

Thread in TCP

Week 6
Program Studi Teknik Informatika
Fakultas Teknik – Universitas Surabaya
What is Thread?
What is Thread?
• Short for a thread of execution. Threads are a way for a program to
divide (termed "split") itself into two or more simultaneously (or
pseudo-simultaneously) running tasks.
• Java is the first programming language that use threading in this
environment

• Example Application:
– Access browser with multiple tabs
– Webserver handles many request from multiple PC/device

• Source: https://simple.wikipedia.org/wiki/Thread_(computer_science)
LifeCycle a Thread
Thread Status “new”

Thread Status
“Runnable”
LifeCycle a Thread
"Runnable" threads may change to
"timed waiting" for a certain period.
Status will return to "runnable" when
the time is up

The thread will be "waiting" if the


thread is waiting for another The thread "runnable"
thread to finish first. The thread becomes "terminated" if
will return "runnable" when the the already running task
other thread is finished is completed
Thread Priority
• Each thread in Java has a priority which helps the operating system
determine which thread to run first
• Java thread priority is in the range between MIN_PRIORITY
(constant 1) and MAX_PRIORITY (constant 10). By default, each
thread is given NORM_PRIORITY priority (constant 5).
• Threads with higher priority must be allocated by the processor
before threads with lower priority are executed.
• However, thread priority cannot guarantee thread execution order
and is highly platform dependent.
Single Thread
• Single-Threading is a very lightweight
process that has one thread as a controller

• Multi-Threading is a process by having


multiple threads and doing more than one
task simultaneously
Implementation (1)
15 Minute

1. implements Runnable
2. extends Thread
Using “implements Runnable”
• Create a class "Worker“ that implements runnable
Using “implements Runnable”
• Make the function "default" run
• Make the function start ()
Using “implements Runnable”
Using “extends Thread”
• Create a class "WorkerThread“
extends Thread
Using “extends Thread”
What is the difference
between "Runnable
implements" and
"Extends Thread"?
Implementation (2)
30 Minute
Simple Chat Using TCP
• Open the previous
project, namely simple
chat (Week 6)

• Added a thread function


that functions to "multiple
chat" at the same time
Server Side
• Add Runnable
implements in Form
Server
• Add an object t that
comes from the Thread.
• Make sure the import is
correct
Server Side
• Add a run () function
because the thread must
run the run function.

• Don't forget to add a try


and catch
Server Side
• Move “object s and incoming = s.accept();" in the getChat () function to the
FormServer default constructor and in the default constructor add a thread
function call
Server Side
• Remove the getChat() function in the actionperformed click the send
button
Client Side
• Add Runnable implements in
Client Form
• Add an object t that comes from
the Thread.
• Make sure the import is correct
Client Side
• Add a run () function because the
thread must run the run function.

• Don't forget to add a try and


catch

• Add the getMessage () function


Client Side
• Move some of the code in
btn send to getMessage ()
• The transfer is so that the
client can receive messages
at any time with any
condition
Client Side
Delete this line
and move it to
FormClient ()

The result of
moving the Baris ini berguna untuk
clientSocket object membuat thread dan
menjalankannya
RESULT
Any Question ?

You might also like