Wikipedia:Reference desk/Archives/Computing/2012 November 27
Computing desk | ||
---|---|---|
< November 26 | << Oct | November | Dec >> | November 28 > |
Welcome to the Wikipedia Computing Reference Desk Archives |
---|
The page you are currently viewing is an archive page. While you can leave answers for any questions shown below, please ask new questions on one of the current reference desk pages. |
November 27
[edit]thread synchronization-1
[edit]Hi! my doubt is on thread synchronization.
when a thread is accesing a method which is synchronized that method should not be available to any other thread until the first thread
completes execution.
But below code not doing so.
you please read and clear my doubts.
class Table
{
synchronized void printTable(int n)
{for(int i=1;i<=5;i++)
{
System.out.println(n*i);
try
{
Thread.sleep(500);
}
catch(Exception ie)
{
System.out.println(ie);
}
}
}
}
class MyThread1 extends Thread
{
Table t;
MyThread1(Table t)
{
this.t=t;
}
public void run()
{
t.printTable(5);
}
}
class MyThread2 extends Thread
{
Table t;
MyThread2(Table t)
{
this.t=t;
}
public void run()
{
t.printTable(100);
}
}
class synchronization2
{
public static void main(String args[])
{
MyThread1 t1=new MyThread1(new Table());
MyThread2 t2=new MyThread2(new Table());
t1.start();
t2.start();
}
}
Expected out put:
5
10
15
20
25
100
200
300
400
500
but the actual output is
5
100
200
10
300
15
400
20
500
25
my doubts:
1)why it is not giving expected output?
2)what is the mistake in the programme?
2)what can i do to get the expected out put?
— Preceding unsigned comment added by Phanihup (talk • contribs) 02:23, 27 November 2012 (UTC)
- Don't you get bored asking the same question over and over again, and just ignoring the answers people gave you? It's the same as #java yield method and yield method in java. In both cases people pointed you to tutorials about java thread synchronization. Yet here you are again, a month later, and you've ignored all that advice. 87.112.97.202 (talk) 02:33, 27 November 2012 (UTC)
- Monitors in java are associated with objects. To coordinate two threads with one another, they must synchronise against the same object, and thus off the same monitor. When you put synchronized in the signature of a method, that means "synchronise on this". In your example, because you've got two instances of Table, there are two objects, and so you're creating two monitors. So the two threads aren't synchronizing off the same monitor, and so they're not coordinated. You could either just alter your code to use a single table (which you pass to both MyThread constructors) or you can explicitly synchronize on a static object (or any singleton). It's a common ideom to synchronize on the class object common to the things you want to synchronize. So that would make printTable look like:
class Table {
void printTable(int n) {
// synchronise on a static (the class object) which is shared between all the instances of Table
synchronized(Table.class){
for(int i=1; i<=5; i++) {
System.out.println(n*i);
try {
Thread.sleep(500);
}
catch(Exception ie) {
System.out.println(ie);
}
}
}
}
}
- The above is true for object methods (that is, non-static methods). When you say synchronized on a class method (that is, a static method) they get synchronised on the class instance automatically. So, if it suited your code, you could instead keep the code you have (and not have that explicitly synchronized block) but instead make printTable a class method:
static synchronized void printTable(int n) {
- I should note that, in practical examples, it's very seldom a wise idea to sleep while you're holding a monitor, as it unproductively blocks other threads trying to enter that monitor for the duration of the sleep, as java.lang.Thread.sleep(long) retains the monitor during the sleep. (But I do realise your example isn't supposed to be real code). -- Finlay McWalterჷTalk 15:42, 27 November 2012 (UTC)
i want some time — Preceding unsigned comment added by Me shankara (talk • contribs) 08:24, 28 November 2012 (UTC) your explanation is correct Finlay McWalter.My doubt is cleared.thanks you sir! — Preceding unsigned comment added by Phanihup (talk • contribs) 09:32, 29 November 2012 (UTC)
Specific Website blocking for predetermined time
[edit]Hello. It's great to be back...
Anyway, I am in need of something which will block specific sites for my own computer. Say if I wanted to not access facebook, I could be able to set the time amount which it would be blocked, and after the time has passed, I would be able to access it. It needs to be compatible with all browsers (so no browser extensions). It also needs to be free. I've looked at what seems to be a popular solutions with parents (I'm actually a teenager trying to actually do work when I need to) such as K9 Web Protection, but it allows one to enter a password to allow access. It needs to block it for a certain amount of time, regardless whether I want to go back on facebook or not. Any suggestions? — Preceding unsigned comment added by 122.108.156.85 (talk) 03:43, 27 November 2012 (UTC)
- I think someone asked a similar question before. You might want to check the archives for answers. One solution would be to modify the hosts file on your computer so that the website gets a bogus addresses assigned, which will result in an error message if you try to access it. See hosts (file). You could then have a program run at certain times that would put the modified host file into place and then at a later time it would put back the host file that allows the website access. Every OS should have a way to launch the program on a timer. On Linux, you could use cron. On Windows you can use the Task Scheduler. RudolfRed (talk) 03:55, 27 November 2012 (UTC)
- K9 Web Protection works on both Windows and Macintosh, which OS do you use? There is one site that specializes in geeky methods to stop you from slacking off: Lifehacker.com [1][2][3][4] Trio The Punch (talk) 06:27, 27 November 2012 (UTC) p.s. For the pro's: Focused Time and iNet Protector.
Thank's Trio, your number 5 link was just what I needed. Not cross browser and easy to change with a click of a button, but the best so far. Thank you! 122.108.156.85 (talk) 08:13, 28 November 2012 (UTC)
C recognizes sqrt except for once
[edit]I use sqrt several times, and only once do I get the error: undefined reference to 'sqrt'
int distanceBetween (struct Point p, struct Point q)
{
return sqrt(pow((q.y - p.y ), 2.0) + pow((q.x - p.x ),2.0));
}
Why am I getting that error in just this one function? 128.111.43.35 (talk) 04:34, 27 November 2012 (UTC)
- Some compilers or linkers will only report one error for each missing function. RudolfRed (talk) 06:28, 27 November 2012 (UTC)
- And the solution to this problem is probably to
#include <math.h>
and to link and/or compile with-lm
. --Stephan Schulz (talk) 07:15, 27 November 2012 (UTC)
- And the solution to this problem is probably to
- By the way the hypot function does most of that work though not all systems support it. And if you want to square a number x^2 is more obvious and faster. Dmcq (talk) 09:33, 27 November 2012 (UTC)
- I think you mean x*x - ^ is the binary XOR operator in C. AndrewWTaylor (talk) 10:08, 27 November 2012 (UTC)
- Yes you're right, stupid me - I've just using another language, I also use ** sometimes. Dmcq (talk) 23:16, 27 November 2012 (UTC)
- I think you mean x*x - ^ is the binary XOR operator in C. AndrewWTaylor (talk) 10:08, 27 November 2012 (UTC)
- By the way the hypot function does most of that work though not all systems support it. And if you want to square a number x^2 is more obvious and faster. Dmcq (talk) 09:33, 27 November 2012 (UTC)
pari/gp or possibly bash problem
[edit]Hi. kubuntu linux; bash; gp version 2.5.3. If I type
echo 'vector(600,i,i)' | gp -q
it works as expected but
echo 'vector(700,i,i)' | gp -q
gives undesired output: it truncates and puts three plus signs. I want the full vector. How do I make gp produce what I want? Robinh (talk) 09:07, 27 November 2012 (UTC)
- http://pari.math.u-bordeaux.fr/dochtml/gpman.html ¦ Reisio (talk) 18:04, 27 November 2012 (UTC)
- (OP) thanks for this, Reisio. There doesn't seem to be a flag that solves my problem. Looks like I'll have to write to the pari team. Best wishes, Robinh (talk) 22:01, 27 November 2012 (UTC)
i Have 2 Vids which play in my Pc in Mono sounding
[edit]when i play them, i hear sound only from one amplifier. my amplifier are fine, this happens only with the vids. what can i do? — Preceding unsigned comment added by 79.182.153.70 (talk) 16:12, 27 November 2012 (UTC)
- Figure out how to make your player send a single channel to both speakers, or how to re-encode the video to do the same. ¦ Reisio (talk) 18:00, 27 November 2012 (UTC)
- Hi, i don;t have the tools to figure it out. — Preceding unsigned comment added by 79.182.153.70 (talk) 19:42, 27 November 2012 (UTC)
- You could tray by checking the option "speakers fill"
- Iskánder Vigoa Pérez (talk) 03:52, 28 November 2012 (UTC)