类A中有一个Hashtable,存了一些文件路径(key为文件路径)。然后我在类A中建立n个线程(n<10)。
因为要在线程中传递参数,所以我设置了类B,类B中执行的是添加一些文件路径的操作,也是想添加回类A的Hashtable中,但是可能有重复的路径,所以我想检测类A中的Hashtable是否有重复。问题是我想线程在异步的情况下,每个线程都能把新添加的路径加入到A类的Hashtable,而且根据更新过的A类的Hashtable去判断文件路径是否重复。
class A
{
private Hashtable hPath=new Hashtable;
...
Thread[] thread=new Thread[5];
for(...)
{
B ab=new B(hPath);
thread[i]=new ThreadStart(B.addPath);
...
}
...
}class B
{
private Hashtable hash=new Hashtable();
public B(Hashtable hash)
{
h=hash;
} public void addPath()
{
//监测路径是否重复,添加无重复路径
if(!h.ContainsKey(spath))
{
}
} //不知道如何把h给回A类中的hPath??????
}不知道我的表述是否有问题?如有问题可以立即提出,希望大家能尽快帮我解决!谢谢!!
因为要在线程中传递参数,所以我设置了类B,类B中执行的是添加一些文件路径的操作,也是想添加回类A的Hashtable中,但是可能有重复的路径,所以我想检测类A中的Hashtable是否有重复。问题是我想线程在异步的情况下,每个线程都能把新添加的路径加入到A类的Hashtable,而且根据更新过的A类的Hashtable去判断文件路径是否重复。
class A
{
private Hashtable hPath=new Hashtable;
...
Thread[] thread=new Thread[5];
for(...)
{
B ab=new B(hPath);
thread[i]=new ThreadStart(B.addPath);
...
}
...
}class B
{
private Hashtable hash=new Hashtable();
public B(Hashtable hash)
{
h=hash;
} public void addPath()
{
//监测路径是否重复,添加无重复路径
if(!h.ContainsKey(spath))
{
}
} //不知道如何把h给回A类中的hPath??????
}不知道我的表述是否有问题?如有问题可以立即提出,希望大家能尽快帮我解决!谢谢!!
{
private Hashtable hPath=new Hashtable;
...
Thread[] thread=new Thread[5];
for(...)
{
B ab=new B(hPath);
thread[i]=new ThreadStart(B.addPath);
...
}
...
}class B
{
private Hashtable h; //注意这里,在构造函数中给他添加A类中的hPath引用
public B(Hashtable hash)
{
h=hash;
} public void addPath()
{
//监测路径是否重复,添加无重复路径
lock(h)//注意这里,加上lock,使引用变量进入临界区,保证线程安全
{
if(!h.ContainsKey(spath))
{
h.Add();//直接加就行了。
}
}
}}
解决,要分!!!!!!!!!!!
public class Multithreading
{
public Multithreading(){} private System.Windows.Forms.Timer timer2=new System.Windows.Forms.Timer();
private int sf;
private int bf;
private Hashtable neschPath=new Hashtable();
private int sign1=0;
private int sign2=0;
private ThreadProc tp=null;
private ThreadProc tp2=null;
private ArrayList alSort=new ArrayList();
public void callThread()
{
Console.WriteLine("//////////////////////////////////////");
for(int i=20;i<100;i++)//为了测试预先装入20-100的数
{
neschPath.Add(i.ToString(),"");
Console.WriteLine(i.ToString());
}
Console.WriteLine("//////////////////////////////////////");
Console.WriteLine();
Console.WriteLine();
Thread[] th=new Thread[2]; sf=30;
bf=150;//测试重复装入,以及多线程的异步、共享一个Hashtable
tp=new ThreadProc(sf,bf,ref neschPath);
th[0]=new Thread(new ThreadStart(tp.CheckStatus));
sf=0;
bf=190;;//测试重复装入,以及多线程的异步、共享一个Hashtable
tp2=new ThreadProc(sf,bf,ref neschPath);
th[1]=new Thread(new ThreadStart(tp2.CheckStatus));
th[0].Start();
th[1].Start(); Console.WriteLine();
Console.WriteLine();
Console.WriteLine("//////////////////////////////////////");
timer2.Tick += new System.EventHandler(this.timer2_Tick);
timer2.Enabled=true;//因为无法预测线程几时完成,所以等待,不知道谁有更好
//的方法?
} private void timer2_Tick(object sender, System.EventArgs e)
{
try
{
if(tp!=null&&tp2!=null)
{
sign1=tp._sign;
sign2=tp2._sign;
if(sign1==1&&sign2==1)
{
Console.WriteLine();
Console.WriteLine();
Console.WriteLine
("//////////////////////////////////////");
Console.WriteLine("out sort");
IDictionaryEnumerator myEnumerator =
neschPath.GetEnumerator();
while ( myEnumerator.MoveNext() )
{
alSort.Add(Int32.Parse
(myEnumerator.Key.ToString()));
}
alSort.Sort();
for(int i=0;i<alSort.Count;i++)
{
Console.WriteLine(alSort[i].ToString());
}
((System.Windows.Forms.Timer)sender).Enabled=false;
((System.Windows.Forms.Timer)sender).Dispose();
}
}
}
catch(Exception es)
{
Console.WriteLine(es.ToString());
}
} } public class ThreadProc
{
private Hashtable h=new Hashtable();
private int sf;
private int bf;
public int _sign=0; public ThreadProc(int s,int b,ref Hashtable hadPath)
{
h=hadPath;
sf=s;
bf=b;
} public void CheckStatus()
{
for(int i=sf;i<bf;i++)
{
lock(h)//使引用变量进入临界区,保证线程安全
{
if(!h.ContainsKey(i.ToString()))
{
h.Add(i.ToString(),"");
Console.WriteLine(i.ToString());
}
}
}
_sign=1;
}
}很多不符合规范的东西,希望高手指正!