Life cycle of a thread
(Tap the post to see more)
Life cycle of a thread
A thread goes through various stages in
its lifecycle. The following diagram
shows the complete life cycle of a thread.
New
Runnable
Waiting
Terminated
(Dead)
Labels: Java
We've moved! — MindVault360 is now SrcForge. Better design, more content & premium notes.
We've upgraded to SrcForge — a faster, more professional platform with better content, premium notes, and a modern design.
Visit us at SrcForge
Mindvault360 is your all-in-one knowledge hub for programming, coding, ethical hacking, cybersecurity, PC tricks, and accounting. Explore expert tutorials, troubleshooting guides, security insights, and tech hacks to boost your skills. Whether you're a coder, tech enthusiast, or finance learner, Mindvault360 delivers valuable resources to expand your knowledge and stay ahead in the digital world
(Tap the post to see more)
Life cycle of a thread
A thread goes through various stages in
its lifecycle. The following diagram
shows the complete life cycle of a thread.
New
Runnable
Waiting
Terminated
(Dead)
Labels: Java
(Tap the post to see more)
Multithreading
•
Multithreading is a concept where a program is
divided into two or more subprograms, which can be executed at the same time in
parallel. This is also known as multithreading.
•
A thread is similar to a program that has a
single flow of control
•
The process
of executing multiple threads simultaneously is known as multithreading.
Advantages
of Multithreading
Steps
to Create a thread in java
•
create a new class that extends Thread, then
override the run() method and then to create an instance of that class.
•
The run() method is what is executed by
the thread after you call start().
•
Here is an example of creating a Java
Thread subclass:
public class MyClass extends Thread
{
public
void run()
{
System.out.println("MyClass
running");
}
}
To create and start the above thread:
MyClass t1 = new MyClass ();
T1.start();
When the run() method executes it will
print out the text " MyClass
running ".
Example
program to create three threads and display the strings using multithreading
concept
class A extends Thread
{
public
void run()
{
for(int
i=1;i<=5;i++)
System.out.println(“Welcome
to C”);
}
}
class B extends Thread
{
public
void run()
{
for(int
i=1;i<=5;i++)
System.out.println(“Welcome
to C++”);
}
}
class C extends Thread
{
public
void run()
{
for(int
i=1;i<=5;i++)
System.out.println(“Welcome
to Java”);
}
}
class threaddemo
{
public
static void main(String args[])
{
A
a=new A();
a.start();
B
b=new B();
b.start();
C
c=new C();
c.start();
}
}
Example 02:
package com.java.Multi_threading;
// Class that implements Runnable for creating a thread
class Task1 implements Runnable {
public void run() {
for (int i = 1; i <= 5; i++) {
System.out.println("Task 1 - Counting: " + i);
try {
Thread.sleep(500); // Simulating some work with sleep
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
// Another class that implements Runnable for creating a thread
class Task2 implements Runnable {
public void run() {
for (int i = 1; i <= 5; i++) {
System.out.println("Task 2 - Counting: " + i);
try {
Thread.sleep(500); // Simulating some work with sleep
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
// Main class to run the threads
class ThreadDemo {
public static void main(String[] args) {
// Creating instances of Runnable classes
Task1 task1 = new Task1();
Task2 task2 = new Task2();
// Creating threads from Runnable instances
Thread thread1 = new Thread(task1);
Thread thread2 = new Thread(task2);
// Starting the threads
thread1.start();
thread2.start();
// Main thread work
for (int i = 1; i <= 5; i++) {
System.out.println("Main Thread - Counting: " + i);
try {
Thread.sleep(500); // Simulating some work with sleep
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
package com.java.Multi_threading;
class Table{
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);}
}
}
}
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);
}
}
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();
}
}
Labels: Java
Input/Output (I/O)
functions in C are used to perform input and output operations between a
program and the user or the system. Some of the commonly used I/O functions in
C are:
printf(): This
function is used to print formatted output to the console or terminal. It takes
a format string and a list of arguments, which are used to format the output.
Example 01:
#include
<stdio.h>
int main() {
char name[] = "John";
int age = 25;
printf("My name is %s and I am %d years
old.\n", name, age);
return 0;
}
Output: My name is John and I am 25
years old.
scanf():
This function is used to read formatted input from the console or terminal. It
takes a format string and a list of pointers to the variables where the input
should be stored.
Example 02:
#include
<stdio.h>
int main() {
char name[20];
int age;
printf("Enter your name and age:
");
scanf("%s %d", name, &age);
printf("Your name is %s and you are %d
years old.\n", name, age);
return 0;
}
Output:
Enter your name and
age: John 25
Your name is John and
you are 25 years old.
getchar():
This function is used to read a single character from the console or terminal.
It waits for the user to press a key and returns the ASCII code of the
character.
Example 03:
#include
<stdio.h>
int main() {
char c;
printf("Enter a character: ");
c = getchar();
printf("You entered the character %c,
which has ASCII code %d.\n", c, c);
return 0;
}
Output:
Enter a character: A
You entered the
character A, which has ASCII code 65.
putchar():
This function is used to write a single character to the console or terminal.
It takes an ASCII code as an argument and writes the corresponding character.
Example 04:
#include
<stdio.h>
int main() {
char c = 'A';
putchar(c);
return 0;
}
Output: A
gets():
This function is used to read a string of characters from the console or
terminal. It reads characters from the input until it encounters a newline
character or the end of the input.
Example 05:
#include
<stdio.h>
int main() {
char name[20];
printf("Enter your name: ");
gets(name);
printf("Your name is %s.\n",
name);
return 0;
}
Output:
Enter your name: John
Your name is John.
Note: The gets()
function is considered unsafe because it can cause a buffer overflow if the
input string is longer than the size of the buffer.
It is recommended to
use the fgets() function instead.
#include
<stdio.h>
int main() {
FILE *fp;
fp = fopen("file.txt",
"w");
fprintf(fp, "This is some text in the
file.\n");
fclose(fp);
return 0;
}
In this example, the
file "file.txt" is opened in write mode ("w") using the
fopen() function. The fprintf()
function is then used to write some text to the file. Finally, the fclose()
function is used to close the file.
Perform input/output
operations: Once the file is opened, you can perform input/output operations on
the file using functions such as fgetc(), fgets(), fputc(), fputs(),
fprintf(), and fscanf().
Example 07:
#include
<stdio.h>
int main() {
FILE *fp;
char buffer[20];
fp = fopen("file.txt",
"r");
fgets(buffer, 20, fp);
printf("The text in the file is:
%s\n", buffer);
fclose(fp);
return 0;
}
In this example, the
file "file.txt" is opened in read mode ("r") using the
fopen() function. The fgets() function is then used to read a line of text from
the file into the buffer. Finally, the printf() function is used to print the text
that was read from the file.
Close the file:
Once you have finished performing input/output operations on the file, you
should close the file using the fclose() function.
Example 08:
#include
<stdio.h>
int main() {
FILE *fp;
fp = fopen("file.txt",
"a");
fprintf(fp, "This text is appended to
the file.\n");
fclose(fp);
return 0;
}
In this example, the
file "file.txt" is opened in append mode ("a") using the
fopen() function. The fprintf() function is then used to append some text to
the end of the file. Finally, the fclose() function is used to close the file.
Note: It is important
to check whether the file was opened successfully before performing
input/output operations on the file. The fopen() function returns NULL if it is
unable to open the file, for example if the file does not exist or if the user
does not have permission to access the file.
Labels: C