Click here to Skip to main content
15,892,674 members
Home / Discussions / C#
   

C#

 
AnswerRe: Timer Pin
Colin Angus Mackay12-Apr-06 20:38
Colin Angus Mackay12-Apr-06 20:38 
GeneralRe: Timer Pin
rahultaing12-Apr-06 21:05
rahultaing12-Apr-06 21:05 
AnswerRe: Timer Pin
rahultaing12-Apr-06 21:06
rahultaing12-Apr-06 21:06 
AnswerRe: Timer Pin
Corinna John12-Apr-06 21:07
Corinna John12-Apr-06 21:07 
AnswerRe: Timer Pin
V.12-Apr-06 21:19
professionalV.12-Apr-06 21:19 
Questionvideo player in c# Pin
ramyasangeet12-Apr-06 20:22
ramyasangeet12-Apr-06 20:22 
QuestionHelp regarding IKVMC tool Pin
sathyasivam12-Apr-06 20:03
sathyasivam12-Apr-06 20:03 
QuestionThread Synchronization program Pin
eric_tran12-Apr-06 19:42
eric_tran12-Apr-06 19:42 
Please show your ability!

I'd like to use 5 threads running concurrently. They use the same method AccessData to add 2 random numbers in the ArrayList of 1000 elements.

Is there anything wrong in my coding?
How to update the ArrayList after each thread?
How to terminate if there is only 1 number left in the ArrayList?

Thanks so much

 using System;<br />
using System.Collections;<br />
using System.Text;<br />
using System.Threading;<br />
<br />
namespace A1_SS<br />
{<br />
    class Program<br />
    {<br />
        static object lockObject;<br />
        static ArrayList space;<br />
<br />
        static void Main()<br />
        {<br />
<br />
            space = new ArrayList();<br />
<br />
            for (int i = 1; i <= 1000; i++)<br />
            {<br />
                space.Add(i);<br />
<br />
            }<br />
<br />
            lockObject = new object();<br />
<br />
            <br />
                Thread t1 = new Thread(new ParameterizedThreadStart(Program.AccessData));<br />
                t1.Start(space);<br />
<br />
                Thread t2 = new Thread(new ParameterizedThreadStart(Program.AccessData));<br />
                t2.Start(space);<br />
<br />
                Thread t3 = new Thread(new ParameterizedThreadStart(Program.AccessData));<br />
                t3.Start(space);<br />
<br />
                Thread t4 = new Thread(new ParameterizedThreadStart(Program.AccessData));<br />
                t4.Start(space);<br />
<br />
                Thread t5 = new Thread(new ParameterizedThreadStart(Program.AccessData));<br />
                t5.Start(space);<br />
            }<br />
<br />
            Thread.Sleep(20 * 1000);<br />
        }<br />
<br />
<br />
<br />
        public static void AccessData(object param)<br />
        {             <br />
<br />
            ArrayList list = (ArrayList)param;<br />
<br />
            lock (lockObject)<br />
            {<br />
                <br />
<br />
                    Random rand = new Random();<br />
<br />
                    int x = rand.Next(1, list.Count);<br />
                    list.Remove(x);<br />
                    Console.WriteLine("x = " + x);<br />
<br />
                    int y = rand.Next(1, list.Count);<br />
                    list.Remove(y);<br />
                    Console.WriteLine("y = " + y);<br />
<br />
                    int newNumber = x + y;<br />
                    Console.WriteLine("newNumber = " + newNumber);<br />
<br />
                    list.Add(newNumber);<br />
<br />
<br />
<br />
                    foreach (object obj in list)<br />
                        Console.WriteLine(obj);<br />
                <br />
<br />
            }<br />
<br />
<br />
<br />
        }<br />
    }<br />
}<br />


eric
AnswerRe: Thread Synchronization program Pin
Tehnoon12-Apr-06 20:02
Tehnoon12-Apr-06 20:02 
GeneralRe: Thread Synchronization program Pin
eric_tran12-Apr-06 20:51
eric_tran12-Apr-06 20:51 
GeneralRe: Thread Synchronization program Pin
eric_tran12-Apr-06 20:55
eric_tran12-Apr-06 20:55 
QuestionHow to reach the (Name) property Pin
Calin Macrinici12-Apr-06 18:13
Calin Macrinici12-Apr-06 18:13 
AnswerRe: How to reach the (Name) property Pin
Tehnoon12-Apr-06 19:40
Tehnoon12-Apr-06 19:40 
GeneralRe: How to reach the (Name) property Pin
Calin Macrinici13-Apr-06 3:41
Calin Macrinici13-Apr-06 3:41 
QuestionHow to change column width in crystalreport Pin
LongNguyen12-Apr-06 17:46
LongNguyen12-Apr-06 17:46 
AnswerRe: How to change column width in crystalreport Pin
vamsimohan2112-Apr-06 17:56
vamsimohan2112-Apr-06 17:56 
QuestionFlickering on derived button with transparancy Pin
techt12-Apr-06 14:39
techt12-Apr-06 14:39 
AnswerRe: Flickering on derived button with transparancy Pin
alexey N12-Apr-06 18:46
alexey N12-Apr-06 18:46 
QuestionTreeList View Items Pin
ang2612-Apr-06 14:14
ang2612-Apr-06 14:14 
Questioncrystal report problem Pin
shabonaa12-Apr-06 11:52
shabonaa12-Apr-06 11:52 
AnswerRe: crystal report problem Pin
vamsimohan2112-Apr-06 17:49
vamsimohan2112-Apr-06 17:49 
GeneralRe: crystal report problem Pin
shabonaa12-Apr-06 21:14
shabonaa12-Apr-06 21:14 
Questionstrange exception caused by DirectX? Pin
melanieab12-Apr-06 10:30
melanieab12-Apr-06 10:30 
AnswerRe: strange exception caused by DirectX? Pin
Judah Gabriel Himango12-Apr-06 10:59
sponsorJudah Gabriel Himango12-Apr-06 10:59 
AnswerRe: strange exception caused by DirectX? Pin
mav.northwind12-Apr-06 20:02
mav.northwind12-Apr-06 20:02 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.