473,463 Members | 1,495 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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 1636
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...
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
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.