Wikipedia:Reference desk/Archives/Computing/2012 December 9
Computing desk | ||
---|---|---|
< December 8 | << Nov | December | Jan >> | December 10 > |
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. |
December 9
[edit]need a programme on synchronized block in java
[edit]the general form of synchronized block is
class table { ....... void printTable(int n) { synchronized(obj) { ...... } } }
In the above code,i wrote synchronized(obj).Here obj means object of table class or it may be object of a string class or object of
Integer class or it may be any object of any class.
If we write this in place of obj it means it is object of table class.That means we are geting lock on table class.
In below programme I got lock on table class by putting this in place of obj.
But I am not able to write a programme to get lock on object of other than table class.
I want a programme which gets lock on object other than table class.
can any one give such type of programme?
you no need to write entire programme.you simply edit below programme.
import java.io.*; class table { void printTable(int n) { synchronized(this) { for(int i=1;i<=5;i++) { System.out.println(n*i); try{ Thread.sleep(500); } catch(InterruptedException 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 synchronizedblock1 { public static void main(String args[] ) { table t=new table(); MyThread1 t1=new MyThread1(t); MyThread2 t2=new MyThread2(t); t1.start(); t2.start(); } } ========================== output: 5 10 15 20 25 100 200 300 400
- Learn to indent your code. The above is a bit difficult to read. You can, for example, simply create a dummy class like:
class Dummy { }
- Then you can give an object of this class as a constructor parameter to your class
table
and synchronise on this object:
class table { private Dummy d; public table(Dummy d) { this.d=d; } public void doStuff() { synchronized (d) { } } //rest of code goes here }
- I am not going to write the entire code for you but you should get the general idea here. JIP | Talk 07:31, 9 December 2012 (UTC)
- I already answered this at Wikipedia:Reference desk/Archives/Computing/2012 November 27#thread synchronization-1 (without dummy whatnots). -- Finlay McWalterჷTalk 13:58, 9 December 2012 (UTC)
Thanks for your response.But my doubt is unsolved.
what I am asking is "How to write a programme by locking an object in table class other than table class."
If we lock object of table class it means "no two objects of table class can't access synchronized block of table class "simultaneously."
According to JIP if we lock d,what is meant by ' getting lock on d'?which objects are trying to acces d .
with out the objects of table class how can we aceess table class block?
Like this I have somany doubts.So please write the entire programme which locks object of a class other than table class.
you no need to write entire code.you simplyedit the code which i gave.I hope you help me.
- The objects that lock on
d
are yourtable
objects inMyThread1
andMyThread2
. When you have:
table t = new table();
- you can instead use the following:
Dummy d = new Dummy(); table t = new table(d);
- then you just construct
MyThread1 t1
andMyThread2 t2
like you did above. The example code I wrote above allows thetable
object to lock on some other object than itself, which is what you seem to have asked. Remember that for this to be of any use, both threads need to lock on the same object. JIP | Talk 19:52, 10 December 2012 (UTC)
Resizing partitions on Linux without losing data?
[edit]This morning I decided to do a system update to my Fedora 17 Linux system by typing yum update
. The update failed, because the file system /boot
needs 3 MB more free space. How can I resize my HD partitions without losing the data on them? JIP | Talk 08:48, 9 December 2012 (UTC)
- The Parted Magic LiveCD (or LiveUSB) has all the tools you need, and their website has all the documentation on how to use said tools. WegianWarrior (talk) 09:37, 9 December 2012 (UTC)
- I also had missized a /boot partition once. If repartitioning it doesn't work (it often won't) you may just want to move the old boot kernel and sysmap, etc., to a different partition. I've been doing that for a while. Unless you boot into an old kernel... in which case you'll have problems. Shadowjams (talk) 18:33, 9 December 2012 (UTC)
- You may actually have tons of old kernels laying around there, if the updater works like others I have used. Move (or just delete) some of the oldest kernels. 209.131.76.183 (talk) 17:07, 10 December 2012 (UTC)
- Yes, it appears I do have multiple kernels there. Here's the output of
ls -l /boot
:
- Yes, it appears I do have multiple kernels there. Here's the output of
- You may actually have tons of old kernels laying around there, if the updater works like others I have used. Move (or just delete) some of the oldest kernels. 209.131.76.183 (talk) 17:07, 10 December 2012 (UTC)
- I also had missized a /boot partition once. If repartitioning it doesn't work (it often won't) you may just want to move the old boot kernel and sysmap, etc., to a different partition. I've been doing that for a while. Unless you boot into an old kernel... in which case you'll have problems. Shadowjams (talk) 18:33, 9 December 2012 (UTC)
total 48203 -rw-r--r--. 1 root root 119103 2012-08-21 22:24 config-3.5.2-3.fc17.x86_64 -rw-r--r--. 1 root root 122022 2012-10-10 15:31 config-3.6.1-1.fc17.x86_64 drwxr-xr-x. 2 root root 1024 2012-10-12 08:27 grub drwxr-xr-x. 6 root root 1024 2012-10-12 08:33 grub2 -rw-r--r--. 1 root root 16997871 2012-09-01 16:27 initramfs-3.5.2-3.fc17.x86_64.img -rw-r--r--. 1 root root 17502517 2012-10-12 08:33 initramfs-3.6.1-1.fc17.x86_64.img drwx------. 2 root root 12288 2012-09-01 15:58 lost+found -rw-------. 1 root root 2468248 2012-08-21 22:24 System.map-3.5.2-3.fc17.x86_64 -rw-------. 1 root root 2504428 2012-10-10 15:31 System.map-3.6.1-1.fc17.x86_64 -rwxr-xr-x. 1 root root 4782720 2012-08-21 22:24 vmlinuz-3.5.2-3.fc17.x86_64 -rwxr-xr-x. 1 root root 4831632 2012-10-10 15:31 vmlinuz-3.6.1-1.fc17.x86_64
uname -a
reports:Linux teletran-1 3.6.1-1.fc17.x86_64 #1 SMP Wed Oct 10 12:13:05 UTC 2012 x86_64 x86_64 x86_64 GNU/Linux
Which of the above files can I safely delete? JIP | Talk 19:56, 10 December 2012 (UTC)- Can't you just do this from the package manager? It appears you have two kernel versions installed (3.5.2 and 3.6.1). I assume both show up in the GRUB startup menu. Can't you just uninstall the one you don't use, presumably the older one? --NorwegianBlue talk 21:25, 10 December 2012 (UTC)
rpm -qa | grep kernel
reports this:
- Can't you just do this from the package manager? It appears you have two kernel versions installed (3.5.2 and 3.6.1). I assume both show up in the GRUB startup menu. Can't you just uninstall the one you don't use, presumably the older one? --NorwegianBlue talk 21:25, 10 December 2012 (UTC)
kernel-tools-3.6.1-1.fc17.x86_64 kernel-tools-libs-3.6.1-1.fc17.x86_64 kernel-headers-3.6.1-1.fc17.x86_64 libreport-plugin-kerneloops-2.0.14-1.fc17.x86_64 abrt-addon-kerneloops-2.0.13-1.fc17.x86_64 kernel-3.5.2-3.fc17.x86_64 kernel-3.6.1-1.fc17.x86_64
- Which of these can I remove? JIP | Talk 18:21, 11 December 2012 (UTC)
- Presumably you're running 3.6.1 without any issues. I would remove 3.5.2. 209.131.76.183 (talk) 18:26, 13 December 2012 (UTC)
- Yes, I am running 3.6.1. So I just do
rpm -e
for any package containing "kernel
" and "3.5.2
" in its name? JIP | Talk 19:47, 13 December 2012 (UTC)- Who would have thought? That command worked, and when I rebooted, I got back to kernel 3.6.1 all fine, and was able to run
yum update
. JIP | Talk 08:28, 15 December 2012 (UTC)
- Who would have thought? That command worked, and when I rebooted, I got back to kernel 3.6.1 all fine, and was able to run
- Yes, I am running 3.6.1. So I just do
Good opportunity to abandon a separate /boot
. Copy the /boot
directory to the /
partition, re-run grub-install
, modify /etc/fstab
, done. ¦ Reisio (talk) 22:45, 9 December 2012 (UTC)
How to exponentiate BigInteger values in Java?
[edit]I am trying to compute a power of two in Java using BigIntegers. The line that eclipse doesn't like is the following:
public class JScanner_main {
....
BigInteger two = new BigInteger("2");
BigInteger exponent = lowbound;
BigInteger poweroftwo = two.pow(exponent);
....
Eclipse says
The method pow(int) in the type BigInteger is not applicable for the arguments (BigInteger)
What exactly is the problem here? Does that mean exponent cannot be a BigInteger? How can I solve this? -- Toshio Yamaguchi 13:10, 9 December 2012 (UTC)
- No, for pow() the only acceptable argument type is int, per the BigInteger documentation. You can do this with modPow, as long as you supply a mod value that you know will be larger than the result:
import java.math.*;
public class big {
public static final void main(String [] args){
BigInteger b1 = new BigInteger ("2");
System.out.println("2^^8 is " + b1.modPow(new BigInteger("8"),
new BigInteger("100000000000000000000000")));
}
}
- But to need that you'll need to be calculating a value > 2230, and I think you'll find BigInteger will chew through lots of time and memory to do that - see low long this takes just to calculate that value:
System.out.println("2 to 2^^30 = " + new BigInteger("2").pow(1073741824));
- Well, what I want to do is computing 2n for large values of n. I guess I better use multiply then and use a loop to compute the power. -- Toshio Yamaguchi 13:58, 9 December 2012 (UTC)
- Storing the decimal expansion of 2230 will take about 200 Mbytes (100 Mbytes if they use BCD, but I don't think they do), so 2235 will take about 6 Gbytes to store (handwavy calculations here). You don't have enough memory to deal with decimals this gigantic. -- Finlay McWalterჷTalk 14:21, 9 December 2012 (UTC)
- How large are the values of n ? Is n always a positive integer ? And does the answer need to be exact, right down to the one's position ? StuRat (talk) 16:47, 9 December 2012 (UTC)
- I want to systematically test all even integers for a congruence as part of investigating a mathematical problem. I don't have any exact plans so far, but testing up to around 1015 or something would be nice. -- Toshio Yamaguchi 19:13, 9 December 2012 (UTC)
- 1015 is less than 250, so you don't need to be raising a BigInteger to the power of another BigInteger. You can just use the existing BigInteger.pow(int) call. -- Finlay McWalterჷTalk 19:39, 9 December 2012 (UTC)
- Do you mean n = 1015 ? That's makes 2n so large it won't fit on any computer. If, on the other hand, you mean that 2n = 1015, then that's 125 GB, which is huge, but could fit on a hard drive. So, if you need to write out numbers that large, in full detail, it is theoretically possible, but it won't be quick. Do you need the exact number, or is some approximation acceptable ? StuRat (talk) 04:39, 10 December 2012 (UTC)
- Please say what your actual goal is. This often has a huge impact on the algorithm you should use. If you for example want to search for Wieferich primes or near-Wieferich primes then computation of large powers of two is irrelevant for any meaningful search. Instead you should use modular exponentiation which will never compute values larger than p4 when you test p. Java has a modPow function for it. PrimeHunter (talk) 05:43, 10 December 2012 (UTC)
- 1015 will fit in a 64-bit regular int and doesn't need bignums, but as PrimeHunter says, you should probably say what you're actually trying to calculate (and the math ref desk may be more helpful than here). If you can test a billion numbers a second you're looking at a week or so of testing, if you want to test all the even numbers up to 10**15. But there very well may be optimizations possible. 66.127.54.40 (talk) 07:04, 10 December 2012 (UTC)
- I plan to write a program to search for solutions of the congruence 2n ≡ 1 (mod (n + 1)2) (up to about n = 1015 or something, which I know will result in huge intermediate values in such a search area). I plan to use Java BigInteger data type for that. I haven't developed a specific algorithm for this task yet (another user earlier suggested repeated squaring modulo (n+1)2 might be useful). -- Toshio Yamaguchi 12:31, 10 December 2012 (UTC)
- If you're just planning on doing this the obvious brute-force way, then I think modpow is exactly what you're looking for. Basically (psuedocode) two.modpow(n, (n+1)^2) == 1, assuming I understand the problem correctly - it has been a long time since I have actually done anything with modulo arithmetic. 209.131.76.183 (talk) 12:39, 10 December 2012 (UTC)
- Modular exponentiation is definitely the way to go here. It would be very slow if the large powers of two were computed and tested directly. With p = n+1 you ask for 2p-1 ≡ 1 (mod p2). If this holds then we also have the weaker 2p-1 ≡ 1 (mod p). So p must either be an odd prime (which always satisfies the weaker condition by Fermat's little theorem), or a base 2 Fermat pseudoprime. The prime solutions are the Wieferich primes. PrimeGrid's Wieferich Prime Search currently says Completed Thru 692761e11.[1] If you are also interested in pseudoprime solutions then you can download all base 2 Fermat pseudoprimes below 1015 at http://www.cecm.sfu.ca/Pseudoprimes/. OEIS:A001567 has those below 1012. PrimeHunter (talk) 23:31, 10 December 2012 (UTC)
- Hmm, my math is weak these days but I think that equality holds when (n+1) divides the order of the multiplicative group Z/(n+1)^2Z, i.e. (n+1) divides the Euler totient function of (n+1)^2. You can figure out the totient from the prime factorization of n+1 and (just guessing) you have higher chances of a match when n+1 is made of small factors. So you might get a more efficient search by constructing suitable n's instead of just exhaustively searching them. Do you know if the congruence even has any solutions? You might be able to make a probabilistic guess of how often they occur (maybe never) based on the distribution of prime factorizations of numbers. 66.127.54.40 (talk) 00:05, 11 December 2012 (UTC)
- I definitely expect to find two solutions below 10000, namely 1092 and 3510. I don't think those numbers are base 2 pseudoprimes (A001567 doesn't list them). It is easy to see that if p is a Wieferich prime, then p-1 satisfies this congruence, but I don't know whether the numbers one less than Wieferich primes are the only solutions. For example, there might be solutions where n+1 is composite, although I believe that would mean any prime factor of n+1 would be a Wieferich prime and I don't know whether such numbers exist (if my conjecture about n+1 composite is correct, then the only prime factors smaller than about 7×1016 are 1093 and 3511, according to the latest search result from PrimeGrid here). -- Toshio Yamaguchi 07:04, 11 December 2012 (UTC)
- Hm, interesting, I guess this problem is nontrivial. I just checked all even numbers up to 1e8 (took about 1.5 minutes on one core of my old 2.5 ghz Core 2 laptop) and 1092 and 3510 were the only matches. With a newer computer and a more careful implementation maybe you can get 10x speedup, so around 1e7/sec ignoring slowdowns as the numbers get bigger. Let's say 5e7 using multicore. So to get through 1e15 you need 2e7 seconds, or most of a year. Maybe there is some way to use a GPU. I think a naive Java program won't be optimal. 66.127.54.40 (talk) 08:35, 11 December 2012 (UTC)
- I definitely expect to find two solutions below 10000, namely 1092 and 3510. I don't think those numbers are base 2 pseudoprimes (A001567 doesn't list them). It is easy to see that if p is a Wieferich prime, then p-1 satisfies this congruence, but I don't know whether the numbers one less than Wieferich primes are the only solutions. For example, there might be solutions where n+1 is composite, although I believe that would mean any prime factor of n+1 would be a Wieferich prime and I don't know whether such numbers exist (if my conjecture about n+1 composite is correct, then the only prime factors smaller than about 7×1016 are 1093 and 3511, according to the latest search result from PrimeGrid here). -- Toshio Yamaguchi 07:04, 11 December 2012 (UTC)
- I wrote about p = n+1 and explained that p must either be an odd prime or a base 2 Fermat pseudoprime. I didn't claim n had to be a pseudoprime. Prime p has already been tested by PrimeGrid up to 692761e11 = 6.92761×1016. The only numbers n you have to test up to 1015 are the 1801533 numbers for which n+1 is listed in the file at http://www.cecm.sfu.ca/Pseudoprimes/. This can be done in a minute. If you have problems with the file format then I can do it for you. Larger databases of base 2 pseudoprimes have also been computed but I don't know whether they are online. PrimeHunter (talk) 13:59, 11 December 2012 (UTC)
- I think you are right that if p is a composite solution then the prime factors are Wieferich primes. There are such cases for many other bases than 2. For example base 26 where the solutions up to 107 of 26p-1 ≡ 1 (mod p2) are: 3, 5, 15=3×5, 71, 1065=3×5×71. PrimeHunter (talk) 03:22, 12 December 2012 (UTC)
Online store doesn't work with Firefox 16, but works with other browsers
[edit]This web store (warning: potentially NSFW content) isn't working in Firefox 16. Every time I put something in my shopping basket and try to go to the checkout, it tells me the shopping basket is empty. The exact same store works OK in Epiphany and in Midori. I haven't tested it on MSIE because Microsoft doesn't make software for Linux. Can anyone tell me what's the problem here? JIP | Talk 17:30, 9 December 2012 (UTC)
- What happens when you try Firefox in safe mode? -- Finlay McWalterჷTalk 17:32, 9 December 2012 (UTC)
- The exact same thing. I can select as many items as I like but I can never get to pay for them. JIP | Talk 17:35, 9 December 2012 (UTC)
- WFM. Where’d you get your Firefox build? Try 17. ¦ Reisio (talk) 20:02, 9 December 2012 (UTC)
- I got it from the Fedora 17 operating system. I updated to 17 but the problem still exists. JIP | Talk 06:15, 10 December 2012 (UTC)
- Well since it works on my Linux box, my first guess is that there's something up with your build or the configuration you're using with it. One thing you might try is to get the binary build of Firefox from mozilla.com directly — if that works, it's even more likely there's some issue with Fedora's build/configuration, and you'd be even more justified filing a bug (at http://bugzilla.redhat.com/ I'm guessing). ¦ Reisio (talk) 00:14, 14 December 2012 (UTC)
- I got it from the Fedora 17 operating system. I updated to 17 but the problem still exists. JIP | Talk 06:15, 10 December 2012 (UTC)
- OMG, what kind of shop is that? JIP | Talk likes having his ass spanked. JIP, be a good boy and use another browser. OsmanRF34 (talk) 00:22, 10 December 2012 (UTC)
- No, I was there for the BoundCon tickets, actually. JIP | Talk 06:15, 10 December 2012 (UTC)
The problem might have been a corrupted cookie. I deleted all cookies from that site, which resulted in the site claiming my browser was rejecting cookies. I brought up the entire Firefox history, searched for the site's name, and clicked "Forget about this site". When I quit and restarted Firefox, and went to the site again, the store seemed to work OK. JIP | Talk 08:59, 15 December 2012 (UTC)