0% found this document useful (0 votes)
17 views25 pages

5.CoreJavaProgramming Day5

This document provides an introduction to core Java programming and multi-threading concepts. It discusses the history and evolution of Java, data types, operators, control statements, classes, interfaces, exceptions, generics, collections, streams and lambda expressions. It also covers creating simple GUIs using Swing and implementing multi-threading in Java programs.

Uploaded by

khairymahmoud795
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
0% found this document useful (0 votes)
17 views25 pages

5.CoreJavaProgramming Day5

This document provides an introduction to core Java programming and multi-threading concepts. It discusses the history and evolution of Java, data types, operators, control statements, classes, interfaces, exceptions, generics, collections, streams and lambda expressions. It also covers creating simple GUIs using Swing and implementing multi-threading in Java programs.

Uploaded by

khairymahmoud795
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1/ 25

CORE JAVA

PROGRAMMING
AN INTRODUCTION TO JAVA

Presented By
Dr. Eman Hesham, DBA.
Agenda
A. The History and Evolution of Java
B. An Overview of Java
C. Data Types, Variables and Arrays
D. Operators, Control Statements and String Handling
E. Modifiers, Access Specifiers, Packages and Interfaces
F. Wrapper Classes, Autoboxing and Inner Classes
G. Exception Handling
H. Generics and Lambda Expressions and Method Reference
I. Java Stream API
J. Java Collections
K. Multi-Threading in Java
Lesson 11
Multi-Threading in Java
Create Simple GUI – java Swing Frame

 JFrame is a class of the javax.swing package .

 This is the top-level window, with border and a title bar.

 JFrame class has various methods which can be used to customize it.
Create Simple GUI – java Swing Frame
Create Simple GUI – java Swing Panel

 JPanel, a part of the Java Swing package, is a container that can store a group of
components.

 The main task of JPanel is to organize components, various layouts can be set in
JPanel which provide better organization of components.
Create Simple GUI – java Swing Panel

1
Create Simple GUI

2
Multi-
Threading
What is Thread?
 A Thread is
 A single sequential execution path in a program
 Used when we need to execute two or more program segments concurrently (multitasking).
 Used in many applications:
 Games, animation, perform I/O
 Every program has at least two threads.
 Each thread has its own stack, priority & virtual set of registers.

 Multiple threads does not mean that they execute in parallel when you’re working in a single
CPU.
 Some kind of scheduling algorithm is used to manage the threads (e.g. Round Robin).
 The scheduling algorithm is JVM specific (i.e. depending on the scheduling algorithm of the
underlying operating system)
Threads
 several thread objects that are executing concurrently:
Main Thread

Garbage
Collection th1
Event Dispatcher
Thread Thread
th2 run()

th3

run()

run()

Daemon Threads (System) User Created Threads


Threads
 Threads that are ready for execution are put in the ready queue.

 Only one thread is executing at a time, while the others are waiting for their turn.

 The task that the thread carries out is written inside the run() method.
The Thread Class

 Class Thread
 start()
 run()
 sleep()*
 suspend()*
 resume()*
 stop()*
Working with Threads
Thread
 There are two ways to work with threads:

 Extending Class Thread: start()


run() { }
1. Define a class that extends Thread. …

2. Override its run() method.


3. In main or any other method: MyThread
a. Create an object of the subclass.
b. Call method start().
run() {
…………
}
Working with Threads
Thread

1
public class MyThread extends Thread
start()
{
run() { }
public void run() 2 …
{ • in main() or any method:
… //write the job here
MyThread
}
} public void anyMethod()
{
MyThread th = new MyThread(); 3.a run() {
th.start(); 3.
b
…………
}
}
Working with Threads

 There are two ways to work with threads:

 Implementing Interface Runnable:


1. Define a class that implements Runnable.
2. Override its run() method .
3. In main or any other method:
a. Create an object of your class.
b. Create an object of class Thread by passing your object to the
constructor that requires a parameter of type Runnable.
c. Call method start() on the Thread object.
Working with Threads
Runnable

1 void run();
class MyTask implements Runnable
{
public void run() 2
MyTask Thread
{
… //write the job here void run(){
} 3 ……… Thread()
public void anyMethod() } Thread(Runnable r)
} start()
{
run() { }
MyTask task = new MyTask(); a
Thread th = new Thread(task); b
th.start(); c
} • in main() or any method
Extending Thread VS. Implementing Runnable

 Choosing between these two is a matter of taste.


 Implementing the Runnable interface:
 May take more work since we still:
 Declare a Thread object
 Call the Thread methods on this object
 Your class can still extend other class

 Extending the Thread class


 Easier to implement
 Your class can no longer extend any other class
Example:
Example:
Example:
Create Simple GUI –
java Swing Panel with Thread
Lab Exercises
Lab Exercise 1
 Simple Date and Time JFrame Application.
Lab Exercise 2
 Make a text marquee (is a scrolling piece of text displayed horizontally across

your App.

You might also like