Sie sind auf Seite 1von 30

Introduction to Java

From: Gul Mohammad


SaferTech

Agenda

Why Java
Java Architecture
Java Platforms
Distinctive features of java
Multi threading
Frameworks
What can do in java

Why java
Java is the most trusted language for building an enterprise level
application.

1. Google's (Gmail & YouTube) is built on Java/some other lang.


2. Twitter (Natty) & LinkedIn uses Java & Scala.
3. Amazon, EBay, Blogger uses almost Java.
4. Java is potentially the best programming languages which is both
highly secure and better at performance
5. Java has a highly supported open source community.
6. Literally, whole Financial sector blindly prefers it and many
more
7. Last but not least, 3 billion devices run Java.

Java architecture

Java Platforms

Java Platform, Standard Edition (Java SE)


Java Platform, Enterprise Edition (Java EE)
Java Platform, Micro Edition (Java ME)
JavaFX

JVM,JDK,JRE
This is important to understand JVM, JDK and
JRE.

JVM
JVM (Java Virtual Machine) is an abstract machine. It is a specification that
provides runtime environment in which java bytecode can be executed.
JVMs are available for many hardware and software platforms. JVM, JRE
and JDK are platform dependent because configuration of each OS differs.
But, Java is platform independent.
The JVM performs following main tasks:
Loads code
Verifies code
Executes code
Provides runtime environment

JRE (Java Runtime


Environment)
It is used to provide runtime environment. It is the implementation of JVM. It physically
exists. It contains set of libraries + other files that JVM uses at runtime.

JVM Architecture

Cont
1) Classloader:
Classloader is a subsystem of JVM that is used to load class files.

2) Class(Method) Area:
Class(Method) Area stores per-class structures such as the runtime constant pool,
field and method data, the code for methods.

3) Heap:
It is the runtime data area in which objects are allocated.

4)Stack:
Java Stack stores frames. It holds local variables and partial results, and plays
a part in method invocation and return.
Each thread has a private JVM stack, created at the same time as thread.
A new frame is created each time a method is invoked. A frame is destroyed
when its method invocation completes.

Cont..
5) Program Counter Register:
PC (program counter) register. It contains the address of the Java virtual machine
instruction currently being executed.
6) Native Method Stack:
It contains all the native methods used in the application.
7) Execution Engine:
It contains:
A virtual processor
Interpreter: Read bytecode stream then execute the instructions.
Just-In-Time(JIT) compiler: It is used to improve the performance. JIT compiles parts of the
byte code that have similar functionality at the same time, and hence reduces the amount
of time needed for compilation. Here the term ?compiler? refers to a translator from the
instruction set of a Java virtual machine (JVM) to the instruction set of a specific CPU.

JDK (Java Development Kit)


It physically exists. It contains JRE +
development tools.

Distinctive features of Java

Architectural natural
Simple
Platform Independent
Pure Object Oriented
Distributed
Secure

No memory pointers
Programs runs inside the virtual machine sandbox.
Array index limit checking
bytecode verifier checks classes after loading
class loader- confines objects to unique namespaces. Prevents loading a hacked
"java.lang.SecurityManager" class, for example.
security manager - determines what resources a class can access such as reading and
writing to the local disk.

Compiled and interpreted, Robust , Strong memory allocation , garbage


collection, Portable

Other Powerful features

Instrumentation
Socket programming
Multithreading
Powerful IO
Rich Data structure framework

Multithreading
Multitasking:
Refers to a computer's ability to perform multiple jobs
concurrently
More than one program are running concurrently, e.g., UNIX

Multithreading:
A thread is a single sequence of execution within a program
Refers to multiple threads of control within a single program
Each program can run multiple threads of control within it,
e.g., Web Browser

Concurrency vs. Parallelism


CPU

CPU1

CPU2

Threads and Processes


CPU

main
run
Process 1

Process 2

Process 3

GC

Process 4

What are Threads Good For?


To maintain responsiveness of an
application during a long running task
To enable cancellation of separable
tasks
Some problems are intrinsically
parallel
To monitor status of some resource
(e.g., DB)
Some APIs and systems demand it

Application Thread

When we execute an application:


1. The JVM creates a Thread object whose task is
defined by the main() method
2. The JVM starts the thread
3. The thread executes the statements of the
program one by one
4. After executing all the statements, the method
returns and the thread dies

Multiple Threads in an Application


Each thread has its private run-time stack
If two threads execute the same method, each will
have its own copy of the local variables the
methods uses
However, all threads see the same dynamic
memory, i.e., heap (are there variables on the
heap?)
Two different threads can act on the same object
and same static fields concurrently

Scheduling Threads
start()

Ready queue

Newly created
threads
Currently executed
thread
I/O operation completes
What happens when
a program with a
ServerSocket calls
accept()?

Waiting for I/O operation to be


Waiting to be notified
Sleeping
Waiting to enter a synchronized

completed
section

Thread State Diagram


Alive
Running
new ThreadExample();

while () { }

New Thread

Runnable

thread.start();

Dead Thread
run() method returns

Blocked

Object.wait()
Thread.sleep()
blocking IO call
waiting on a monitor

Scheduling

Thread scheduling is the mechanism


used to determine how runnable
threads are allocated CPU time
A thread-scheduling mechanism is
either preemptive or nonpreemptive

Preemptive Scheduling
Preemptive scheduling the thread
scheduler preempts (pauses) a running
thread to allow different threads to execute
Nonpreemptive scheduling the scheduler
never interrupts a running thread
The nonpreemptive scheduler relies on the
running thread to yield control of the CPU so
that other threads may execute

Daemon Threads

Daemon threads are background threads, that provide


services to other threads, e.g., the garbage collection
thread
The Java VM will not exit if non-Daemon threads are
executing
The Java VM will exit if only Daemon threads are executing
Daemon threads die when the Java VM exits
Q: Is the main thread a daemon thread?

Java Frameworks
Frameworksare large bodies (usually many classes) of
prewritten code to which you add your own code to
solve a problem in a specific domain. Perhaps you could
say that the framework uses your code because it is
usually the framework that is in control. You make use of
a framework by calling its methods, inheritance, and
supplying "callbacks", listeners, or other
implementations of theObserverpattern.
Contrast to library. Although sometimes large libraries
are referred to as frameworks, this is probably not the
most common use of the term.

Popular Frameworks

Spring
Struts
Hibernate
Ibaties
JSF
Google web toolkit
Turbine
Apache commons libraries

What can we do in Java

Large scale Enterprise Applications


Service oriented software (Web services)
Web servers
Desktop applications
Game development
Database driven applications
Device oriented programming
Network oriented software (VPN, Remoting)
Security systems
Audio/Video streaming
Distributed computing
Complex algorithms and logics

Who is using java


A very large number of companies are using java

Google
Facebook
eBay
Netflex
Amazone.com
Paypal
Walmart
Yahoo
Flicker
LinkedIn

Thank you

Das könnte Ihnen auch gefallen