473,396 Members | 1,852 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Query regarding Thread.Interrupt

I always though .Net monitors were meant to be similar to thejava synchronisation mechanisms. The code below proves mewrong. When run under the debugger, it produces just "Thread 1"lines, as I expected. But when run without the debugger itproduced both "Thread 1" and "Thread 2" lines, impling that twothreads are executing in the same lock region. I though thatwasn't supposed to happen. Am I wrong?
using System;
using System.Threading;

public class Test
{
Thread t1, t2;

public void Run1()
{
Thread.Sleep(1000);
lock (this)
{
Monitor.Pulse(this);
t2.Interrupt();
for (;;)
Console.WriteLine("Thread 1");
}
}

public void Run2()
{
lock (this)
{
try
{
Monitor.Wait(this);
}
catch (ThreadInterruptedException)
{
Console.WriteLine("2 Interrupted");
}
for (;;)
Console.WriteLine("Thread 2");
}
}

public void Go()
{
t1 = new Thread(new ThreadStart(Run1));
t2 = new Thread(new ThreadStart(Run2));
t1.Start();
t2.Start();
}

public static void Main()
{
new Test().Go();
}
}

--------------------------------
From: Russell Stuart

-----------------------
Posted by a user from .NET 247 (http://www.dotnet247.com/)

<Id>8DcqE4IWO0mDpU3naRwxnw==</Id>
Jul 21 '05 #1
3 1632
Russell Stuart via .NET 247 <an*******@dotnet247.com> wrote:
I always though .Net monitors were meant to be similar to the java
synchronisation mechanisms. The code below proves me wrong. When run
under the debugger, it produces just "Thread 1" lines, as I expected.
But when run without the debugger it produced both "Thread 1" and
"Thread 2" lines, impling that two threads are executing in the same
lock region. I though that wasn't supposed to happen. Am I wrong?


It looks like it's a bug (or at least strange behaviour) of thread
interruption. There's a good reason Thread.interrupt is deprecated in
Java - it's basically a dodgy thing to use, IMO!

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #2
On Wed, 05 May 2004 10:54:10 +0100, Jon Skeet[ C# MVP] wrote:
There's a good reason Thread.interrupt is deprecated in
Java - it's basically a dodgy thing to use, IMO!


Thread.interrupt is not deprecated in Java. Its pretty
safe to use because it re-acquires the lock before
aborting the wait().

Jul 21 '05 #3
Russell Stuart <ru**********@stuart.id.au> wrote:
On Wed, 05 May 2004 10:54:10 +0100, Jon Skeet[ C# MVP] wrote:
There's a good reason Thread.interrupt is deprecated in
Java - it's basically a dodgy thing to use, IMO!


Thread.interrupt is not deprecated in Java. Its pretty
safe to use because it re-acquires the lock before
aborting the wait().


My apologies - I was thinking of Thread.stop(). Doh!

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

38
by: Anthony Baxter | last post by:
On behalf of the Python development team and the Python community, I'm happy to announce the release of Python 2.3.1 (final). Python 2.3.1 is a pure bug fix release of Python 2.3, released in...
26
by: news.microsoft.com | last post by:
Hi, Currently I have a thread thats spinning and doing a Thread.Sleep(someTime). I was thinking of changing this to Thread.Sleep(Timeout.Infinite); then when I have actual data in a...
11
by: Bob Rock | last post by:
Hello, coming from win32 API I recall an ExitThread() call to gently terminate a thread from inside the same thread .... but now all I can see is an Abort call which seems to me a wrapper on the...
20
by: Doug Thews | last post by:
I ran into an interesting re-pain delay after calling the Abort() method on a thread, but it only happens the very first time I call it. Every time afterward, there is no delay. I've got a...
2
by: Sakharam Phapale | last post by:
Hi All, In following example, while playing file, if thread goes in WaitSleepJoin state, due to Thread.Sleep method. Now I want to suspend thread by clicking on cmdSuspend button. I have...
3
by: Russell Stuart via .NET 247 | last post by:
I always though .Net monitors were meant to be similar to thejava synchronisation mechanisms. The code below proves mewrong. When run under the debugger, it produces just "Thread 1"lines, as I...
13
by: LordHog | last post by:
Hello all, I have a little application that needs to poll a device (CAN communications) every 10 to 15 ms otherwise the hardware buffer might overflow when there are message burst on the bus. I...
2
by: =?Utf-8?B?QnJ1Y2UgSFM=?= | last post by:
I'm using VS2005 Winforms. I want a method to allow the user to be able to interrupt an SQL query if he decides he doesn't want to wait for it any longer. I've used IAsyncResult to open an...
6
by: BlueBird | last post by:
Hi, I have a program with a master thread and several slave threads. Whenever an exception occurs, in the master thread or in one of the slave threads, I would like to interrupt all the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.