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. |
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)
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);
}
}
}
}
}
static synchronized void printTable(int n) {
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)
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)
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)
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)
#include <math.h>
and to link and/or compile with -lm
. --Stephan Schulz (talk) 07:15, 27 November 2012 (UTC)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)
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)