Unit 5
Multithreading in Java
Concurrency and Synchronization, Java Thread Model: Thread priorities, Synchronization, Messaging, Main Thread, Creating thread: Implementing Thread using thread class and Runnable interface. Creating multiple threads using isAlive() and join()
Web Based Application in Java: Use of JavaScript for creating web based applications in java, Introduction to javascript frameworks- React, Vue, Angular
SNJB’s Late Sau. K. B. J. College of Engineering
Department of Computer Engineering
Application of Multithreading
SNJB’s Late Sau. K. B. J. College of Engineering
Department of Computer Engineering
SNJB’s Late Sau. K. B. J. College of Engineering
Department of Computer Engineering
SNJB’s Late Sau. K. B. J. College of Engineering
Department of Computer Engineering
SNJB’s Late Sau. K. B. J. College of Engineering
Department of Computer Engineering
SNJB’s Late Sau. K. B. J. College of Engineering
Department of Computer Engineering
SNJB’s Late Sau. K. B. J. College of Engineering
Department of Computer Engineering
SNJB’s Late Sau. K. B. J. College of Engineering
Department of Computer Engineering
SNJB’s Late Sau. K. B. J. College of Engineering
Department of Computer Engineering
SNJB’s Late Sau. K. B. J. College of Engineering
Department of Computer Engineering
SNJB’s Late Sau. K. B. J. College of Engineering
Department of Computer Engineering
class Create extends Thread
{
Create()
{
super("ABC");
start();
}// Constructor Closed
public void run()
{
try {
for(int i = 0;i<5;i++)
{ System.out.println("Child : " +i);
Create Thread by Extending Thread Class
Thread.sleep(500);
} //for close
}catch(InterruptedException e){ }
System.out.println("Child Exiting");
} //run() close
public static void main(String[] args)
{
Create c = new Create();
try {
for(int i = 0;i<5;i++)
{ System.out.println("Mainthread"+i);
Thread.sleep(1000);
} //for close
}catch(InterruptedException e) { }
System.out.println("Main Exiting");
} }
SNJB’s Late Sau. K. B. J. College of Engineering
Department of Computer Engineering
Child : 0
Mainthread0
Child : 1
Mainthread1
Child : 2
Child : 3
Mainthread2
Child : 4
Child Exiting
Mainthread3
Mainthread4
Main Exiting
...Program finished with exit code 0
Press ENTER to exit console.
Create Thread by Extending Thread Class..Output
SNJB’s Late Sau. K. B. J. College of Engineering
Department of Computer Engineering
class Create implements Runnable {
Thread t;
Create()
{
t = new Thread(this,”DemoThread”);
t.start();
}// Constructor Closed
public void run()
{
try {
for(int i = 0;i<5;i++)
{ System.out.println("Child : " +i);
Create Thread by Implementing Runnable
Thread.sleep(500);
} //for close
}catch(InterruptedException e){ }
System.out.println("Child Exiting");
} //run() close
public static void main(String[] args)
{
Create c = new Create();
try {
for(int i = 0;i<5;i++)
{ System.out.println("Mainthread"+i);
Thread.sleep(1000);
} //for close
}catch(InterruptedException e) { }
System.out.println("Main Exiting");
} }
SNJB’s Late Sau. K. B. J. College of Engineering
Department of Computer Engineering
SNJB’s Late Sau. K. B. J. College of Engineering
Department of Computer Engineering
SNJB’s Late Sau. K. B. J. College of Engineering
Department of Computer Engineering
Main Thread
SNJB’s Late Sau. K. B. J. College of Engineering
Department of Computer Engineering
The general form of currentThread method is :
static Thread currentThread();
Hence it can be invoked as follows :
Thread.currentThread();
For Ex :
Main Thread
SNJB’s Late Sau. K. B. J. College of Engineering
Department of Computer Engineering
class Demo
{
public static void main(String args[])
{
Thread t = Thread.currentThread( );
System.out.println(“The Thread is : “ +t);
}
}
O/p : The Thread is : Thread[main,5,main]
In the output Thread[main,5,main] : The order is :
[Name of thread , Priority of thread , & Group of thread]
Main Thread
SNJB’s Late Sau. K. B. J. College of Engineering
Department of Computer Engineering
Main Thread
class Demo
{
public static void main(String args[])
{
Thread t = Thread.currentThread( );
System.out.println(“The Thread is : “ +t);
t.setName(“First Thread”);
System.out.println(“The new name is : “ +t);
int p = t.getPriority( );
System.out.println(“The Thread priority is : “ +p);
t.setPriority(10);
System.out.println(“The new priority is : “ +t.getPriority( ));
}
}
OUTPUT
The Thread is : Thread[main,5,main]
The new name is : [First Thread,5,main]
The Thread priority is : 5
The new priority is : 10
SNJB’s Late Sau. K. B. J. College of Engineering
Department of Computer Engineering
SNJB’s Late Sau. K. B. J. College of Engineering
Department of Computer Engineering
SNJB’s Late Sau. K. B. J. College of Engineering
Department of Computer Engineering
Synchronization
class Table{
synchronized void printTable(int n){
//method not synchronized
for(int i=1;i<=5;i++){
System.out.println(n*i);
try{
Thread.sleep(400);
}catch(Exception e){System.out.println(e);}
} //for close
} }
SNJB’s Late Sau. K. B. J. College of Engineering
Department of Computer Engineering
Synchronization..>Contd
class MyThread1 extends Thread{
Table t;
MyThread1(Table t){
this.t=t;
}
public void run(){
t.printTable(5);
}
}
class MyThread2 extends Thread{
Table t;
MyThread2(Table t){
this.t=t;
}
public void run(){
t.printTable(100);
}
}
SNJB’s Late Sau. K. B. J. College of Engineering
Department of Computer Engineering
class TestSynchronization1{
public static void main(String args[]){
Table obj = new Table();//only one object
MyThread1 t1=new MyThread1(obj);
MyThread2 t2=new MyThread2(obj);
t1.start();
t2.start();
}
}
OUTPUT
5
10
15
20
25
100
200
300
400
500
Synchronization…Contd
SNJB’s Late Sau. K. B. J. College of Engineering
Department of Computer Engineering
wait() | join() |
It is a method of java.lang.Object class. | It is a method of java.lang.Thread |
wait() method can be called by a synchronized block or method. | It is used to stop the current thread. |
wait() method is implemented for performing multi-thread-synchronization. | It can be called either with synchronized and without synchronized context. |
Its syntax is -: public final void wait() throws InterruptedException | Its syntax is -: public final void join() throws InterruptedException |
wait() method causes the thread to sleep until notify() and notifyAll() are called | It can be used to add sequence among more than one thread |
SNJB’s Late Sau. K. B. J. College of Engineering
Department of Computer Engineering
Web Based Application in Java: Use of JavaScript for creating web based applications in java, Introduction to javascript frameworks- React, Vue, Angular
SNJB’s Late Sau. K. B. J. College of Engineering
Department of Computer Engineering
Web Application Development
SNJB’s Late Sau. K. B. J. College of Engineering
Department of Computer Engineering
Static VS Dynamic
SNJB’s Late Sau. K. B. J. College of Engineering
Department of Computer Engineering
Scripts: Server-Side VS Client-Side
SNJB’s Late Sau. K. B. J. College of Engineering
Department of Computer Engineering
JAVASCRIPT
SNJB’s Late Sau. K. B. J. College of Engineering
Department of Computer Engineering
SNJB’s Late Sau. K. B. J. College of Engineering
Department of Computer Engineering
SNJB’s Late Sau. K. B. J. College of Engineering
Department of Computer Engineering
LIBRARY Vs FRAMEWORK
SNJB’s Late Sau. K. B. J. College of Engineering
Department of Computer Engineering
JAVA SCRIPT
SNJB’s Late Sau. K. B. J. College of Engineering
Department of Computer Engineering
JAVASCRIPT FEATURES
SNJB’s Late Sau. K. B. J. College of Engineering
Department of Computer Engineering
NEED/ADVANTAGES OF JAVASCRIPT
SNJB’s Late Sau. K. B. J. College of Engineering
Department of Computer Engineering
Use to structure and give meaning to our web content, for example defining paragraphs, headings, and data tables, or embedding images and videos in the page.
To apply styling to our HTML content, for example setting background colors and fonts, and laying out our content in multiple columns.
To create dynamically updating content, control multimedia, animate images.
SNJB’s Late Sau. K. B. J. College of Engineering
Department of Computer Engineering
<p>Player 1: Chris</p>
p {
font-family: 'helvetica neue', helvetica, sans-serif;
letter-spacing: 1px;
text-transform: uppercase;
text-align: center;
border: 2px solid rgba(0,0,200,0.6);
background: rgba(0,0,200,0.3);
color: rgba(0,0,200,0.6);
box-shadow: 1px 1px 2px rgba(0,0,200,0.4);
border-radius: 10px;
padding: 3px 10px;
display: inline-block;
cursor: pointer;
}
const para = document.querySelector('p');
para.addEventListener('click', updateName);
function updateName() {
const name = prompt('Enter a new name');
para.textContent = `Player 1: ${name}`;
}
SNJB’s Late Sau. K. B. J. College of Engineering
Department of Computer Engineering
JavaScript - Coding Ways/ Script Placement
Script in
<head>...</head> section.
Script in
<body>...</body> section.
Script in
<head>and <body> section.
Script in an external file and then include in <head>...</head> section.
SNJB’s Late Sau. K. B. J. College of Engineering
Department of Computer Engineering
JavaScript - Coding Ways/ Script Placement
Script in
<head>...</head> section.
Script in
<body>...</body> section.
In HTML, JavaScript code is inserted between
<script> and </script> tags.
Script in
<head>and <body> section.
Script in an external file and then include in <head>...</head> section.
Note
SNJB’s Late Sau. K. B. J. College of Engineering
Department of Computer Engineering
JavaScript - Example 1
Script in
<head>...</head> section.
<html>
<head>
<script>
document.write("Hello World")
</script>
</head>
</html>
SNJB’s Late Sau. K. B. J. College of Engineering
Department of Computer Engineering
Example 2
Script in an external file and then include in <head>...</head> section.
<html>
<head>
<script type = "text/javascript" src = "filename.js" ></script>
</head>
<body>
<h2>Demo External JavaScript</h2>
<button type="button" onclick="sayHello()">Try it</button>
</body>
</html>
SNJB’s Late Sau. K. B. J. College of Engineering
Department of Computer Engineering
Example 2 : Output
Script in an external file and then include in <head>...</head> section.
SNJB’s Late Sau. K. B. J. College of Engineering
Department of Computer Engineering
JavaScript - Vue
Open Source
Used to develop interactive web interfaces.
VueJS focusses on the view layer
It is easily integrated into big projects for front-end development without any issues.
SNJB’s Late Sau. K. B. J. College of Engineering
Department of Computer Engineering
<html>
<head>
<title>VueJs Introduction</title>
<script type = "text/javascript" src = "js/vue.js"></script>
</head>
<body>
<div id = "intro" style = "text-align:center;">
<h1>{{ message }}</h1>
</div>
<script type = "text/javascript">
var vue_det = new Vue({
el: '#intro',
data: {
message: 'My first VueJS Task'
}
}); </script> </body> </html>
SNJB’s Late Sau. K. B. J. College of Engineering
Department of Computer Engineering
Example Vue : Output
SNJB’s Late Sau. K. B. J. College of Engineering
Department of Computer Engineering
JavaScript - AngularJs
Powerful JavaScript Framework.
Used in Single Page Application (SPA) projects
Open Source
It extends HTML DOM with additional attributes and makes it more responsive to user actions
SNJB’s Late Sau. K. B. J. College of Engineering
Department of Computer Engineering
JavaScript - AngularJs
It Uses Model View Controller Architecture
AngularJS framework can be divided into three major parts
ng-app
ng-model
ng-bind
SNJB’s Late Sau. K. B. J. College of Engineering
Department of Computer Engineering
JavaScript - AngularJs
ng-app
ng-model
ng-bind
This directive defines and links an AngularJS application to HTML.
This directive binds the values of AngularJS application data to HTML input controls.
This directive binds the AngularJS application data to HTML tags.
SNJB’s Late Sau. K. B. J. College of Engineering
Department of Computer Engineering
<html>
<head>
<title>AngularJS First Application</title>
</head>
<body>
<h1>Sample Application</h1>
<div ng-app = "">
<p>Enter your Name: <input type = "text" ng-model = "name"></p>
<p>Hello <span ng-bind = "name"></span>!</p>
</div>
<script src = "https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js">
</script>
</body>
</html>
SNJB’s Late Sau. K. B. J. College of Engineering
Department of Computer Engineering
SNJB’s Late Sau. K. B. J. College of Engineering
Department of Computer Engineering
SNJB’s Late Sau. K. B. J. College of Engineering
Department of Computer Engineering
SNJB’s Late Sau. K. B. J. College of Engineering
Department of Computer Engineering
SNJB’s Late Sau. K. B. J. College of Engineering
Department of Computer Engineering
End of Unit 5
Thank You!!!
SNJB’s Late Sau. K. B. J. College of Engineering
Department of Computer Engineering