Practice Question PDC
If you do NOT want a thread to be blocked, you can
use Monitor.TryEnter(buffer),
which • acquires the lock and returns true, if the lock is available;
• returns false, if the lock is not available.
• The thread can execute other code, instead of simply
being blocked
using System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Text;
using
System.Threading.Tasks;
using
System.Threading;
namespace PlusAndWait
{
class Program
{
static int count =
0;
bool isfree=true;
public void Sum()
{
/*if (isfree)
{
lock (this)
{
Console.WriteLine("Lock");
for (int i = 0; i < 50;
i++)
{
count += 1;
}
Console.WriteLine("End" + count);
}
isfree = false;
}*/
if (Monitor.TryEnter(this))
{
Console.WriteLine("Try");
for (int i = 0; i
< 10; i++)
{
count += 1;
}
Console.WriteLine("Try End"+count);
return;
}
else
{
Console.WriteLine("Memory Lock Place Wait");
}
}
static void Main(string[] args)
{
Program p = new Program();
Thread t = new Thread(p.Sum);
Thread t1 = new Thread(p.Sum);
t.Start();
t1.Start();
Console.ReadKey();
}
}
}
Producer And Consumer
using System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Text;
using
System.Threading.Tasks;
using
System.Threading;
namespace PlusAndWait
{
class Program
{
static object
totalthread = 1;
static int count =
0;
List<int> number = new List<int>();
int limite=1;
public void
produce()
{
while(true)
{
lock (number)
{
Console.WriteLine(Thread.CurrentThread.Name+"Producer number"+ limite);
number.Add(limite);
if(number.Count==1)
Monitor.PulseAll(number);
if (number.Count == 5)
{
Console.WriteLine(Thread.CurrentThread.Name
+ "Producer wait" +
limite);
Monitor.Wait(number);
limite = 0;
}
Console.WriteLine(Thread.CurrentThread.Name + "Producer end" + limite);
limite += 1;
}
}
}
public void
consumer()
{
while (true)
{
lock (totalthread) {
lock (number)
{
Console.WriteLine(Thread.CurrentThread.Name + "Consumer Lock");
if (number.Count == 0)
{
Console.WriteLine(Thread.CurrentThread.Name
+ "Consumer Wait input number");
Monitor.Wait(number);
}
Console.WriteLine(Thread.CurrentThread.Name + "Consumer number" + number[0]);
Console.WriteLine("consumer" +
number.Count);
Console.WriteLine(Thread.CurrentThread.Name + "Consumer remove " + number[0]);
number.RemoveAt(0);
if (number.Count == 3)
{
Monitor.Pulse(number);
}
}
}
Thread.Sleep(1000);
}
}
static void Main(string[] args)
{
Program p = new Program();
Thread p1 = new Thread(p.produce);
p1.Name = "P1 ";
Thread c1 = new Thread(p.consumer);
c1.Name = "c1 ";
Thread p2 = new Thread(p.produce);
p2.Name = "P2 ";
Thread c2 = new Thread(p.consumer);
c2.Name = "c2 ";
p1.Start();
//p2.Start();
c1.Start();
c2.Start();
//for (int i =
0; i < 10; i++)
//{
// new Thread(p.Sum).Start();
//}
//{
//}
Console.ReadKey();
}
}
}
Delegate Example
using System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Text;
using
System.Threading.Tasks;
namespace Delegate
{
class Delegate
{
public delegate void Calculator(int a, int b);
public static void sum(int a, int b)
{
Console.WriteLine("Sum "+(a + b));
}
public static void sub(int a, int b)
{
Console.WriteLine("Sub " + (a - b));
}
public void mul(int i, int u)
{
Console.WriteLine("mul " + (i * u));
}
static void Main(string[] args)
{
Calculator check = new Calculator(Delegate.sum);
Delegate d = new Delegate();
check += sub;
check += d.mul;
check.Invoke(10, 20);
Console.ReadKey();
}
}
}
Predicate function true and false return
Predicate<int> isPredicate = (int x) => (x % 2 == 0);
Console.WriteLine(isPredicate(10));
Filling
using System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Text;
using
System.Threading.Tasks;
using
System.IO;
namespace weeklab3tahir
{
class Program
{
String readText;
String f1 = @"C:\Users\Muzamil Hafeez\Pictures\BSCS\f1text.txt";
String f2 = @"C:\Users\Muzamil Hafeez\Pictures\BSCS\f2text.txt";
public void
function1()
{
// String path
= @"C:\Users\Muzamil Hafeez\Pictures\BSCS\f1text.txt";
FileStream file = new FileStream(f1, FileMode.Create);
file.Close();
//StreamWriter
writer = new StreamWriter(f1);
//for (int i =
0; i < 10; i++)
//{
// writer.Write("loop " + i);
//}
FileStream file2 = new FileStream(f2,
FileMode.Create,FileAccess.ReadWrite);
file2.Close();
// writer = new
StreamWriter(f1);
//for (int i = 0;
i < 10; i++)
//{
// writer.Write("loop " + i);
//}
}
public void
function2()
{
//String path =
@"C:\Users\Muzamil Hafeez\Pictures\BSCS\f1text.txt";
StreamWriter writer = new StreamWriter(f1);
for (int i = 0; i
< 20; i++)
{
writer.Write("loop 1# " + i);
}
writer.Close();
StreamWriter writer2 = new StreamWriter(f2);
for (int i = 0; i
< 10; i++)
{
writer2.Write("loop 2# " + i);
}
writer2.Close();
}
public void
function3()
{
StreamReader rw = new StreamReader(f1);
{
//readText
= rw.ReadToEnd();
readText= rw.ReadLine();
}
//readText
= File.ReadAllText(f1);
// readText +=
File.ReadAllText(f2);
Console.WriteLine(readText);
/* String[] m = readText.Split('#');
foreach (var item in m)
{
//
Console.WriteLine("+"+item);
Console.WriteLine(item.Replace('9','5'));
}*/
}
static void Main(string[] args)
{
Program run = new Program();
run.function1();
run.function2();
run.function3();
Console.ReadKey();
}
}
}
Comments
Post a Comment