有两个类,所有的控件都是创建在类RWForm里的,然后Book类通过继承该类后,通过RWForm rwf = new RWForm();调用到RWForm中的控件,可以改变其中的值,比如lblTest.Text中明明值已经改变了,但窗口上显示的却还是原来的,控制台输出的却是最新修改过的值,莫名啊RWForm主类:
using System;
using System.Threading;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;namespace OSRWForm
{
public partial class RWForm : Form
{
public RWForm()
{
InitializeComponent();
} private void btnRun_Click(object sender, EventArgs e)
{
Book book = new Book(5);
Random random = new Random(); string[] readerNames = { "读者A", "读者B", "读者C" };
string[] writerNames = { "写者A", "写者B", "写者C" }; Thread[] readerThreads = new Thread[3];
Thread[] writerThreads = new Thread[3]; for (int i = 0; i < 3; i++)
{
Reader reader = new Reader(book, random);
readerThreads[i] = new Thread(new ThreadStart(reader.Read));
readerThreads[i].Name = readerNames[i]; Writer writer = new Writer(book, random);
writerThreads[i] = new Thread(new ThreadStart(writer.Write));
writerThreads[i].Name = writerNames[i]; } for (int i = 0; i < 3; i++)
{
readerThreads[i].Start();
writerThreads[i].Start();
}
} private void btnExit_Click(object sender, EventArgs e)
{
Application.Exit();
}
}
}Book类:
using System;
using System.Threading;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace OSRWForm
{ public class Book : RWForm
{
RWForm rwf = new RWForm();
private int[] pages;
private bool aWriterWait = false;
private int readerCount = 0; private object mutex = new Object(); // 控制对 readerCount 的访问
private object db = new Object(); // 控制对 pages 的访问
private object obj = new Object(); // 控制对 aWriterWait 的访问 public Book(int numOfPages)
{ pages = new int[numOfPages];
} public int NumOfPages
{
get
{
return pages.Length; //常量,无须同步
}
} public int this[int index]
{ get
{
lock (obj)
{
while (aWriterWait)
{
Monitor.Wait(obj);
}
} lock (mutex)
{
// readerCount的锁
// 检查是否为第一个读者
readerCount++;
if (readerCount == 1)
Monitor.Enter(db);
} int valueCopy = pages[index]; // 读取一个数据
rwf.lblTest.Text = "run0";//测试用的我,可是在窗口上这个文本的值并没有改变
Console.WriteLine(rwf.lblTest.Text);// 很奇迹的这里输出却是上面修改过的run,为什么窗口上的文本没改变啊?
rwf.rtxtShow.Text = Thread.CurrentThread.Name + " 读了 第" + (index + 1) + "页,他读到的数据是" + valueCopy;//同样没显示
// Console.WriteLine(Thread.CurrentThread.Name + " 读了 第" + (index + 1) + "页,他读到的数据是" + valueCopy); DisplayContent(); lock (mutex)
{
readerCount--;
if (readerCount == 0)
Monitor.Exit(db);
} return valueCopy;
}
set
{
lock (obj)
{
aWriterWait = true;
} lock (db)
{ // 阻止其他的写者去写书
pages[index] = value; rwf.rtxtShow.Text = Thread.CurrentThread.Name + "把 " + value + " 写到第" + (index + 1) + "页";
Console.WriteLine(Thread.CurrentThread.Name + "把 " + value + " 写到第" + (index + 1) + "页");
DisplayContent();
} lock (obj)
{
aWriterWait = false;
Monitor.PulseAll(obj); // 通知等待的读者
}
}
} private void DisplayContent()
{
Console.WriteLine("{0,-35}", "书的内容是:");
for (int i = 0; i < pages.Length; i++)
{
Console.Write("{0,-9}", pages[i]);
}
Console.WriteLine("\r\n");
}
} public class Reader
{
private Book book;
private Random rand; public Reader(Book b, Random random)
{
book = b;
rand = random;
} public void Read()
{
for (int i = 0; i < 5; i++)
{
int p = rand.Next(0, book.NumOfPages);
int a = book[p];
Thread.Sleep(rand.Next(0, 100));
}
}
}
public class Writer
{
private Book book;
private Random rand; public Writer(Book b, Random random)
{
book = b;
rand = random;
} public void Write()
{
for (int i = 0; i < 5; i++)
{
int p = rand.Next(0, book.NumOfPages);
book[p] = rand.Next(50, 100);
Thread.Sleep(rand.Next(0, 100));
}
}
}}
using System;
using System.Threading;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;namespace OSRWForm
{
public partial class RWForm : Form
{
public RWForm()
{
InitializeComponent();
} private void btnRun_Click(object sender, EventArgs e)
{
Book book = new Book(5);
Random random = new Random(); string[] readerNames = { "读者A", "读者B", "读者C" };
string[] writerNames = { "写者A", "写者B", "写者C" }; Thread[] readerThreads = new Thread[3];
Thread[] writerThreads = new Thread[3]; for (int i = 0; i < 3; i++)
{
Reader reader = new Reader(book, random);
readerThreads[i] = new Thread(new ThreadStart(reader.Read));
readerThreads[i].Name = readerNames[i]; Writer writer = new Writer(book, random);
writerThreads[i] = new Thread(new ThreadStart(writer.Write));
writerThreads[i].Name = writerNames[i]; } for (int i = 0; i < 3; i++)
{
readerThreads[i].Start();
writerThreads[i].Start();
}
} private void btnExit_Click(object sender, EventArgs e)
{
Application.Exit();
}
}
}Book类:
using System;
using System.Threading;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace OSRWForm
{ public class Book : RWForm
{
RWForm rwf = new RWForm();
private int[] pages;
private bool aWriterWait = false;
private int readerCount = 0; private object mutex = new Object(); // 控制对 readerCount 的访问
private object db = new Object(); // 控制对 pages 的访问
private object obj = new Object(); // 控制对 aWriterWait 的访问 public Book(int numOfPages)
{ pages = new int[numOfPages];
} public int NumOfPages
{
get
{
return pages.Length; //常量,无须同步
}
} public int this[int index]
{ get
{
lock (obj)
{
while (aWriterWait)
{
Monitor.Wait(obj);
}
} lock (mutex)
{
// readerCount的锁
// 检查是否为第一个读者
readerCount++;
if (readerCount == 1)
Monitor.Enter(db);
} int valueCopy = pages[index]; // 读取一个数据
rwf.lblTest.Text = "run0";//测试用的我,可是在窗口上这个文本的值并没有改变
Console.WriteLine(rwf.lblTest.Text);// 很奇迹的这里输出却是上面修改过的run,为什么窗口上的文本没改变啊?
rwf.rtxtShow.Text = Thread.CurrentThread.Name + " 读了 第" + (index + 1) + "页,他读到的数据是" + valueCopy;//同样没显示
// Console.WriteLine(Thread.CurrentThread.Name + " 读了 第" + (index + 1) + "页,他读到的数据是" + valueCopy); DisplayContent(); lock (mutex)
{
readerCount--;
if (readerCount == 0)
Monitor.Exit(db);
} return valueCopy;
}
set
{
lock (obj)
{
aWriterWait = true;
} lock (db)
{ // 阻止其他的写者去写书
pages[index] = value; rwf.rtxtShow.Text = Thread.CurrentThread.Name + "把 " + value + " 写到第" + (index + 1) + "页";
Console.WriteLine(Thread.CurrentThread.Name + "把 " + value + " 写到第" + (index + 1) + "页");
DisplayContent();
} lock (obj)
{
aWriterWait = false;
Monitor.PulseAll(obj); // 通知等待的读者
}
}
} private void DisplayContent()
{
Console.WriteLine("{0,-35}", "书的内容是:");
for (int i = 0; i < pages.Length; i++)
{
Console.Write("{0,-9}", pages[i]);
}
Console.WriteLine("\r\n");
}
} public class Reader
{
private Book book;
private Random rand; public Reader(Book b, Random random)
{
book = b;
rand = random;
} public void Read()
{
for (int i = 0; i < 5; i++)
{
int p = rand.Next(0, book.NumOfPages);
int a = book[p];
Thread.Sleep(rand.Next(0, 100));
}
}
}
public class Writer
{
private Book book;
private Random rand; public Writer(Book b, Random random)
{
book = b;
rand = random;
} public void Write()
{
for (int i = 0; i < 5; i++)
{
int p = rand.Next(0, book.NumOfPages);
book[p] = rand.Next(50, 100);
Thread.Sleep(rand.Next(0, 100));
}
}
}}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货