import java.util.concurrent.Semaphore; import java.util.Random; import java.util.Vector; public class Philosopher extends Thread { private static final Random rand = new Random(); private static int event=0; private static String binary=""; private int id; private Semaphore sem; private static Vector
Blog about programming and new technologies in general I've created for myself, to memorize briliant solutions of problems, my own parts of code written previously and some general ideas of workarounds. You can find here Python, Java, JavaScript, C++, Django, jQuery.
June 11, 2010
Threads in Java
"Dining Philosophers" problem solved with Java and Semaphores.
June 8, 2010
Tip of the Day #2 - pointers
Remember
int* ptr; *ptr = 10; #but ptr = new int; #without ampersand and dereference !!
June 7, 2010
Constant pointers, pointers to constants ?
What is the difference between:
float fVar = 3.14; const float* ptr1 = &fVar;and:
float fVar = 3.14; float* const ptr2 = &fVar;? The first one is pointer to constant value, so we can't change the value of variable the pointer points to :
*ptr1 = 10;The second one in contrary is a constant pointer to a value, and because of that we can't readdress it to point at different variable :
float fVar2; *ptr2 = &fVar2;
June 3, 2010
Django Contrib Revealed - Flatpages
Very often while creating big portals there are static pages just wasting your time to be created. Django has a solution for this problem also - the Flatpages application inside Contribution package. This application allows you to manage such static pages from django-admin, and it let's you create templates using Django's template system. How to get started ?
- Add 'django.contrib.flatpages' to INSTALLED_APPS. Also you need to have django.contrib.sites activated since flatpages depend on this package.
- Add 'django.contrib.flatpages.middleware.FlatpageFallbackMiddleware' to MIDDLEWARE_CLASSES.
This is how the flatpage model looks like :
- Add 'django.contrib.flatpages' to INSTALLED_APPS. Also you need to have django.contrib.sites activated since flatpages depend on this package.
- Add 'django.contrib.flatpages.middleware.FlatpageFallbackMiddleware' to MIDDLEWARE_CLASSES.
This is how the flatpage model looks like :
from django.db import models from django.contrib.sites.models import Site class FlatPage(models.Model): url = models.CharField(max_length=100, db_index=True) title = models.CharField(max_length=200) content = models.TextField(blank=True) enable_comments = models.BooleanField() template_name = models.ChorField(max_length=70, blank=True) registration_required = models.BooleanField() sites = models.ManyToManyField(Site)Okay so everything looks clear here I guess. For every flatpage we can specify either custom template, or use flatpages/default.html template (still creating the default template is also our responsibility !). Flatpage templates get a single context variable passed - flatpage, so we can use the Flatpage object's fields ie. {{ flatpage.title }}, {{ flatpage.content }}. Simple, but can spare lots of time :)
Subscribe to:
Posts (Atom)