Your SlideShare is downloading. ×
Lecture 21
Lecture 21
Lecture 21
Lecture 21
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Lecture 21

132

Published on

Notes on core java

Notes on core java

Published in: Technology, Education
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
132
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
12
Comments
0
Likes
0
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. Core Java Debasish Pratihari Multi-Threading :  A multithread program contains two or more parts that can run concurrently. Each part of such program is called a thread, and each thread defines a separate path of execution.  Multithreading is a specialized form of multitasking.  Multithreading enables you to write very efficient programs that make maximum use of the CPU, because idle time can be kept to a minimum. Methods of thread class : getName() getPriority() getId() getThreadGroup() isAlive() join() run() sleep() start() setName() setPriority() setDaemon() yield () interrupt() isDaemon() isInterrupted() Thread Priority :  Java assigns to each thread a priority that determines how that thread should be treated with respect to the others. Thread priorities are integers that specify the relative priority of one thread to another. A higher-priority thread does not run faster than a low priority thread. Instead, a thread priority is used to decide when to switch from one running thread to the next. This is called context switching. The constants to set priority are: MIN_PRIORITY=1 MAX_PRIORITY=10 NORM_PRIORITY=5 Lecture/core/thread2/21 Page #1 feel the Technology…
  • 2. Core Java Debasish Pratihari Thread Synchronization :  When two or more threads need access to a shared resources, they need some way to ensure that the resource will be used by only one thread at a time. The process by which this is achieved is called synchronization.  When a thread invokes a synchronized method , it automatically acquires the intrinsic lock for that method’s object and releases it when the method return. The lock releases occurs even if the return was caused by an un-caught exception. Synchronization Methods : o Synchronized Methods void fun(){ synchronized( expr ) { // 'expr' must evaluate to an Object } } o Synchronized Statement public synchronized void fun() { //method body here } More About Synchronize Key word:  The synchronized keyword is NOT considered part of a method's signature.  It is not automatically inherited when subclasses override super class methods  methods in Interfaces cannot be declared synchronized  constructors cannot be declared synchronized however they can contain synchronized blocks Lecture/core/thread2/21 Page #2 feel the Technology…
  • 3. Core Java Debasish Pratihari Thread Interrupts:  A thread can signal another thread that is should stop executing by calling interrupt() method for that thread object. class MyThread extends Thread{ Thread2(){ start(); } public void run(){ for(int i=1;i<=1000;i++){ System.out.println(i); if(Thread.interrupted()) System.out.println("Thread Inrrupted.."); } } } class ThreadTest{ public static void main(String args[]){ MyThread t= new MyThread (); while(true){ while(!t.isInterrupted()){ t.interrupt(); System.out.println("Interrupted by main"); } } } } Output : Lecture/core/thread2/21 Page #3 feel the Technology…
  • 4. Core Java Debasish Pratihari Thread Group:      Every Java thread is a member of a Thread Group. ThreadGroup facilitate to operate or manage threads as group. Java runtime system creates a Thread Group named “main”. If any thread is created without a thread group specification , the runtime system put the thread into main. Once a thread became a member of a group it can’t be removed from that group. Creating a Thread Group : 25% ThreadGroup debThreads = new ThreadGroup("debasish"); Putting Threads into ThreadGroup: Thread debThread = new Thread(debThreads, this); Getting a Thread’s group: ThreadGroup group = debThread.getThreadGroup(); Example: public class GroupTest{ public static void main(String ar[]){ ThreadGroup group=Thread.currentThread().getThreadGroup(); System.out.println(group.getName()); System.out.println(group.activeCount()); } } Output : main 1 Lecture/core/thread2/21 Page #4 feel the Technology…

×