0% found this document useful (0 votes)
271 views

Thread Lab Assignment

This document contains 4 questions and answers regarding Java threading. Question 1 asks to create and run two threads - one that displays the first 10 even numbers and another that displays a string 10 times. The code provided creates and runs these two threads. Question 2 asks to implement threading by extending the Thread class and implementing the Runnable interface. The code provided creates two classes, one that extends Thread and another that implements Runnable. Question 3 asks to set the priority of the two threads from question 2 and check which executes first. The code sets the priority of one thread higher than the other. Question 4 asks to create an Account class with deposit and withdraw methods, using synchronization for withdrawing from a joint

Uploaded by

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

Thread Lab Assignment

This document contains 4 questions and answers regarding Java threading. Question 1 asks to create and run two threads - one that displays the first 10 even numbers and another that displays a string 10 times. The code provided creates and runs these two threads. Question 2 asks to implement threading by extending the Thread class and implementing the Runnable interface. The code provided creates two classes, one that extends Thread and another that implements Runnable. Question 3 asks to set the priority of the two threads from question 2 and check which executes first. The code sets the priority of one thread higher than the other. Question 4 asks to create an Account class with deposit and withdraw methods, using synchronization for withdrawing from a joint

Uploaded by

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

Q.1.

Write java program to create and run following threads

a. Display first 10 even numbers.


b. Display any String for 10 time.

import java.io.*;
public class Program1
{
public static void main(String args[])throws Exception
{
Thread t1 = new Thread();
Thread t2 = new Thread();
for(int i = 0 ; i<20 ; i=i+2)
{
t1.sleep(100);
System.out.print("\n\t"+i);
}
for( int i = 0 ; i<10 ; i++ )
{
t2.sleep(100);
System.out.print("\n\t Pratik");
}
}
}
Q.2. Write a program to implement the concept of threading by
extending Thread Class and also by implementing interfaces.

Ans:

import java.io.*;
class ExtendThread extends Thread implements Runnable
{
public void run()
{
for( int i = 0 ; i<10 ; i++)
{
try
{
System.out.print("\n\n\t\t Extending Thread...!"+i);
Thread.sleep(100);
}
catch(Exception e)
{}
}
}
}
class ImplementThread implements Runnable
{
public void run()
{
for( int i = 0 ; i<10 ; i++)
{
try
{
System.out.print("\n\n\t\t Implementing Thread...!"+i);
Thread.sleep(100);
}
catch(Exception e)
{}
}
}
}
public class Program2
{
public static void main(String args[])
{
ExtendThread t1 = new ExtendThread();
ImplementThread t2 = new ImplementThread();
t1.start();
t2.run();
}
}
Q.3. Write a program to set the priority of two above threads and
check which thread executes first.
Ans: import java.io.*;
class ExtendThread extends Thread implements Runnable
{
public void run()
{
for( int i = 0 ; i<10 ; i++)
{
try
{
System.out.print("\n\n\t\t Extending Thread...! "+i);
Thread.sleep(100);
}
catch(Exception e)
{}
}
}
}
class ImplementThread implements Runnable
{
public void run()
{
for( int i = 0 ; i<10 ; i++)
{
try
{
System.out.print("\n\n\t\t Implementing Thread...! "+i);
Thread.sleep(100);
}
catch(Exception e)
{}
}
}
}
public class Program3
{
public static void main(String args[])
{
ExtendThread t1 = new ExtendThread();
ImplementThread t2 = new ImplementThread();
t1.setPriority(1);
t1.start();
t2.run();
}
}

Q4. Create an Account class having deposit() & withdraw() methods. Make use of
synchronization while withdrawing money in case of joint account.

import java.io.*;
public class SavingsAccount
{
private double balance;private double interest;
public SavingsAccount()
{
balance = 0;
interest = 0;
}
public SavingsAccount(double initialBalance, double initialInterest)
{
balance = initialBalance;
interest = initialInterest;
}
public void deposit(double amount)
{
balance = balance + amount;
}
public void withdraw(double amount)
{
balance = balance - amount;
}
public void addInterest()
{
balance = balance + balance * interest;
}
public double getBalance()
{
return balance;
}
}

public class SavingsAccountTester


{
public static void main(String[] args)
{
SavingsAccount jimmysSavings = new SavingsAccount(1000, 0.10);
jimmysSavings.withdraw(250);
jimmysSavings.deposit(400);
jimmysSavings.addInterest();
System.out.println(jimmysSavings.getBalance());
System.out.println("Expected: 1265.0");
}
}

You might also like