Multithreading
- Multi means many
Thread means process
- The process indicates either a part of a program or a complete program also
- Multithreading is the concept of executing more than one process simultaneously.
- Multithreading is used to develop parallel processing
Pentium iv 4.3 GHZà43 Lakhs of instructions(cycle ) within 1min Speed
- Sharing CPU cycles efficiently is also called as “Multi-Threading”.
- The operating system allocates a set of as cles based on the priority(importance)of the process.
Ctrl+ Alt+ del
- Based on above statement, the os are divided into two types:-
Interested in mastering .Net Training? Enroll now for a FREE demo on ".Net Training"
Single-Threaded
DOS UNIX
given to one app only
Multi-Threading
- If o/s handles only one process at a time, then it is called as “single thread os”.
- Multi-thread os handles more than one process also
- To work with multi-threading, NET introduced
System. Threading namespace
- Classes of system. Threading namespace
- a) Thread: Used to maintain the life aside of the threads
- b) Thread start: Used to create the threads.
- c) Mutex: Used in thread synchronization
Thread life cycle:-
Four steps for writing threading applications:_
- Write a class with methods
Class test {
Public vaid print(){}
}
- Create an object for thread start delegate with the address of a function
Syntax:-
Test t = new test();
Thread start ts=new thread start(t.print);
- Create a thread with the help of thread start object
Thread tr=new thread(ts);
Start the thread Tr. start()
Example1:-
A program to execute two for loops simultaneously
Open console application project Using system. Threading;
code in GD Class test
{Public static vold p1()
{
For (int i=1; i<=10;i++)
{
Console. write(I+””);
Thread . sleep(500);
}//for
}
//p1
Public static void p2()
{
For(int k=11;k<20;k++)
{
Console. write link(k);
Thread. sleep(500)
//500 millisec
}//for
}
//p2 }//test
Code for the main method:-
Thread start ts1=new thread start (Test.p1)
Thread start ts2=new thread start (Test.p2)
Thread t1=new thread (ts1)
Thread t2=new thread (ts1):
//here
ts1 T1.start() T2.start() Console.Read key();
T1.suspend(); Console. Read key();
T1.Resume(); Console. Read key ();
o/p : 112233----2020
Thread synchronization:-
1.Thread synchronization is the concept of allowing only one thread at a time into one process
2.It process is busy, threads need to wait in the queue
3.Waiting threads will be executed based on priority
- Mutex is a class which controls waiting for threads with the help of wait one() and release mutex()
OBS:
5.The code which is written in between wait one()and release mutex()is called a synchronized code.
A Program to print ”ALL THE BEST”3 TIMES WITH THE HELP OF THREADING:
{Console. write(“ALL”);
Console. write(“THE”);
Console. write(“Best”);
}
open console application project Using system. Threading;
Code in GD
Class program{
Static mutex m = new mutex();
Class test
{
Public static void print()
{
wait one();
console. write(“ALL”);
Thread. sleep(1000);
Consote. write(“THE”);
Thread .sleep(1000);
Console. write(“BEST”);
Thread. sleep(1000);
Console. Write line();
//for new line Thread. sleep(1000);
Release mutex();
}//print }//test
Code for the main method:-
{Thread start ts=new thread start(test. print);
Thread start t1=new thread (ts);
Thread start t2=new thread (ts);
Thread start t3=new thread (ts);
T1.Start(); T2.Start();
T3.Start();
Console. Read key();
}
Pointers:-
Why we use pointers? For fast accessing Fig(a) Accessing ’i’ value is fast when compared to the pointer variable, but it is not in this case fast. Char*p
2B Double*p
Logic is m/c of o/s they are unmanaged code.
The size of the pointers varies or dependent on the o/s, so they are unmanaged code, so c#.net only supports pointers but not implemented.
- Pointer holds the address of a variable.
- The pointer is unmanaged code in c#.net.
- The pointer is also called an unsafe code.
Unsafe is a keyword Pointers logic must be the part of the unsafe block only Pointer size varies from o/s to o/s Generally o/s are divided into 3 types
Unmanaged code is not all recommended In c# .net it we want to use in c# by using un sate keyword
Ex:-
Place a button on the form
{
Un sate { Int iz 90; Int*p; P=&I;
Int k; K=*p**p; MBS(K+””);
}
By pressing f5 it gives a warning that you are using un state code. Please inform to CLR to execute unsafe code.
Go to project
properties
buildàCheck Menu “Allow un sate code” Check Box Execute the project Inter+ view
Internal look-Behavior
subject
time sense
Body language
confidence