Threading

by Jesse 31. July 2008 08:06

Simply put, threading is parallel code execution.  If you have a computer with multiple processors, chances are you have seen the effects of multithreaded programs.  For this code example, there's two calculations done multiple times on different threads.  First one is for Kenetic Energy, Ke = (1/2) * M * V^2 (Kenetic Energy = half mass times velocity squared) and Ohms Law of V = IR (voltage = impedence * resistance). In this example, notice the Kenetic Energy calculation completes before the Ohms Law.

More detail explinations and examples can be found here http://www.albahari.com/threading/.

        const long howManyTimes = 500000000;

        static void Main(string[] args)

        {

            Console.WriteLine("Enter Mass : ");

            double mass = 0;

            double.TryParse(Console.ReadLine(), out mass);

            Console.WriteLine("Enter Velocity : ");

            double velocity = 0;

            double.TryParse(Console.ReadLine(), out velocity);

            //spawn a thread.  This does NOT start the execution of that thread.

            Thread thread = new Thread(delegate() { CalculateKeneticEnergy(mass, velocity); });

            Console.WriteLine("Enter Voltage : ");

            double v = 0;

            double.TryParse(Console.ReadLine(), out v);

            Console.WriteLine("Enter Ohms :");

            double resistance = 0;

            double.TryParse(Console.ReadLine(), out resistance);

            int z = 0;

            double i = 0;

            //start the othe calculation

            thread.Start();

            //Calcuate Amps

            while (z < howManyTimes)

            {

                i = v / resistance;

                z++;

            }

            Console.WriteLine("Amps = " +i.ToString());

           

            //Join tells the host process to wait on other spawned threads, such as this one.

            thread.Join();

            Console.ReadLine();

        }

        static void CalculateKeneticEnergy(double mass, double velocity)

        {

            int i = 0;

            double kE = 0;

            while (i < howManyTimes)

            {

                kE = (0.5 * mass * (velocity * velocity)) / 2;

                i++;

            }

            Console.WriteLine("Kenetic Energy = " + kE.ToString());

        }

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags: , ,

Java - Day 1

by Jesse 31. July 2008 04:14

The only way I ever learn how to use something is to use it outright and not by code examples.  Just doesn't work for me that way.  So I went out and bought "The Complete Reference - Java Seventh Edition" -- it was the only one that had a beginners section, without the "ok, this is a computer".   Just flipping randomly, chapter 11 covers multithreading ooo,  that'll be fun. Chapter 17 covers the Java.util : the collections framework...25 deals with images, fun.  29, "Intorduction to Swing".  Hmm. 

----- Consider items from here on down my notes. ------

I've kinda skimmed over chapter 1-7 so far and I'm on chapter 8.  So far, the syntax is almost 1:1.  Console.WriteLine("string") == System.out.println("string").  Another syntax difference is inheritance.  Inheritance in Java is called "extends".  For instance in C# ...

Public Class Class1{ }

Public Class Class2 : Class1 { }

Class2 would inherit Class1.  In Java, its similiar but different...

class Class1 { }

class Class2 extends Class1 { }

Not that bad, very easy so far.  Method overriding seems to be exactly the same, no difference there.  Same goes for making abstract classes and methods.  There is however a "final" keyword that inhibits overriding.  "final void SayHi();" would stop any inheriting class from overriding that method.

Chapter 9, Packages and interfaces -- Packages are like using includes/usings statements.  I -think- they're DLL style items like in .net, you add them, give a reference in your app, carry on.  Using statements are slightly different (more C/C++ style) -- import java.lang.*; for instance includes everything in the java.lang namespace and like C# you can use a fully qualified name if you wish, such as java.util.Date.  Interfaces are the same too...

interface CallBack {
     void callback(int param);
}

An interesting bit - "you should be careful not to use interfaces casually in performance-critical code" because of the overhead to create the objects.

Imagine that, there's a Try Catch Finally, exactly like in C#, even with the multiple Catch statements (say IOException and Exception).  Same with throws AND threading.  Wow this is becoming annoying, there's even a thread.Join (that means "wait for this thread to complete").

With that, I am stopping @ page 285.  I'm keeping my false sense of empowerment until tomorrow or ...sometime soon.

Currently rated 5.0 by 2 people

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags: ,

You can find anything on the internet ...except people

by Jesse 31. July 2008 03:19

I'm on a search for a particular person.  Please excuse the ambiguity of that, but its the best I can say right now.  I know where this person lives (yep, street address and all), I know a ton of info about them but for the life of me I cannot find something as simple as a phone number, email address (professional or personal), nothing.  Do a search for yourself.  If you're on the net, you'll probably find something within the first page or so.  I come up number 3 on google, number 4 on yahoo and on the 2nd page of Live.  Sometimes I find it useful to narrow it down by including the state, but in this case, since this person is NOT a tech person by any means (not blogging, twittering, myspace/facebook, etc) -- its almost impossible.

I don't know if this person has nerds sittin around ...its possible but increases the complexity of the search.  I understand I could send a letter, but that doesn't mean I get a reply.  Best case, phone call.  Worst case, email. I'm considering releasing this task to the collective (the internet) and just "see what happens".  Hmmm...

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

Java - Day 0

by Jesse 30. July 2008 07:40

Where I work, there's an ...unexpected want for Java developers.  I've never dealt with Java, at all, on any level but some books I've read have said when the cheeze moves, you go with the cheeze.  Now before you want to have me skinned alive, let me say this, I'm not leaving my wonderful world of .Net, at all.  I'm supplimenting and well, I've wanted to learn Java for a while now, so learn I shall.  Why?  I have my reasons and some of them are really freakin cool.

First impression, FINDING the stuff for Java is a pain.  There's no easy "if you are just starting out" area that tells you to just download stuff that you'll figure out later.  Go ahead, take a look and find it.  I'll wait.  Takes forever, but here, download this http://download.netbeans.org/netbeans/6.1/final/ and download "all".  Also, I found over on youtube some video tutorials.  Sweet sweet.

Next, I hopped over to Amazon Jimazon to look for some Java books -- none of which are SE6.  Granted right now I have no idea what  the differences are, maybe its like .net2, 3 and 3.5, but it might be a good idea, I think, to find something thats v6 and thats newer than 2002.  Doh.  Amazon it is.  I'll have to pause this research project for now I guess ...unless I come across something really sweet.

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags: , ,

Ninject

by Jesse 25. July 2008 03:44

If Sitefinity and Subsonic wasn't enough, enter Ninject.  Go to their website to understand what its all about and I took their example and pulled out a bit further.  Take this for instance in the warrior module.  I wanted a swordsman, an archer and a samauri.  I also wanted to attack Steve for landing a consulting gig downtown, so he was the intented target for my new ninject army.

First, I created an archer.  Now before I go further, the example they gave on the ninject site wasn't going to fit what I wanted.  I needed a "base" interface (IWeapon) then subsequent interfaces from there (IRangedWeapon and IMeleeWeapon) so enter IRangedWeapon with Fire (!!!!!) ...

namespace Ninject
{
     public interface IRangeWeapon : IWeapon
    
{
         
void Fire(string target);
    
}
}

And now we need a weapon ...enter the Bow class

namespace Ninject
{
    
public class Bow : IRangeWeapon
    
{
          
void IRangeWeapon.Fire(string target)
           {
Console.WriteLine("Right though {0}'s heart", target); }

           void IWeapon.Hit(string target)
           {
Console.WriteLine("Right though {0}'s heart", target); }
     }
}

along with some extra attributes...(some are not explained in this example)

public class Range : Attribute { }
public class LongSword : Attribute { }
public class ShortSword : Attribute { }
public class LongRange : Attribute { }

And lets make an Archer class...

namespace Ninject
{
class Archer
 
{
  [
Inject, LongRange]
 
public IRangeWeapon Weapon
  {
get; set; }

  [Inject, LongRange]
 
public Archer(IRangeWeapon weapon)
  { Weapon = weapon; }

  [Inject, LongRange]
 
public void Arm(IRangeWeapon weapon)
  { Weapon = weapon; }

  public void Attack(string target)
 
{ Weapon.Hit(target); }

  public void Fire(string target)
 
{ Weapon.Fire(target); }

  }
}

In my module, I changed up the warrior module as such...

public override void Load()
{
     Bind<
IMeleeWeapon>().To<Dagger>().WhereMemberHas<ShortSword>();
     Bind<
IMeleeWeapon>().To<Kitana>().WhereMemberHas<LongSword>();
     Bind<
IRangeWeapon>().To<Shuriken>().WhereMemberHas<Range>();
     Bind<
IRangeWeapon>().To<Bow>().WhereMemberHas<LongRange>();
}

and finally, in the main program I ask for an archer...

namespace Ninject
{
 
class Program
 
{

     const string PersonToAttack = "Steve";

     static void Main(string[] args)
     {
         IKernel kernelWarrior = new StandardKernel(new WarriorModule());
         Samurai warrior = kernelWarrior.Get<Samurai>();
         Archer archer = kernelWarrior.Get<Archer>();
         Swordsman swordsman = kernelWarrior.Get<Swordsman>();
         warrior.Attack(PersonToAttack);
         archer.Attack(PersonToAttack);
         swordsman.Attack(PersonToAttack);

         Console.ReadLine();
     }
  }
}

The result is total mayhem!

Poor steve...he never had a chance...

Ok, you're probably asking "So what?"  Notice my module says WhenMemberHas<T> -- that tells Ninject to go do stuff.  Because the property in Archer is set with the longrange attribute [Inject, LongRange] the kernelWarrior.Get<T> looks at the call finds that attribute and snags in the <Bow>.  There's a bunch of different ways to use this, but hopefully that'll get you started with the million dollar "What else can this thing do?" :-D

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags: , ,

Thinking differently

by Jesse 23. July 2008 08:19

There's some videos I've come across the net.  Not necessarily recently but I found them to be thought provoking.  Take a look.

Shift Happens.

and The Machine is us/ing us

and final video - Pay Attention

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

Weekend at the races

by Jesse 20. July 2008 16:38

No, not nascar, but open wheel, SCCA GT, LeMans GT1, GT2, P1, P2 -- except for the prototypes and indy, they are cars you can go out and buy without all the stickers and ...lack of interior.  I like that kind of racing.  Even better this year, I bought a "super ticket" which gave me all weekend to go and I got a paddock pass so I could go watch the crews work on the cars, even chat with a few of them and meet some of the drivers.  I was there all 3 days and I had a great time.  One thing that caught me off guard was this car... a Porsche GT3

Yep, sponsored by Windows Vista.  Good stuff, and there was two of them.  So back to the paddock area ...how close do they let you get to the cars?  This close.  They had to go to tech to get weighed so they're all legal and allowed to even qualify.  GOOOOD stuff.  I've got a ton of other pictures I took, but ulimately, my ferrari's took 1st in GT2, the Porsche Spyders took 1st in P2 and the Audi reclaims their #1 overall victory position (no surprise really).  I can't wait until next year.

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

off topic

Security will never be easy

by Jesse 14. July 2008 07:27

I've been on a bit of a security throw back the past few days and I've bounced around the web reading, reviewing, relearning some stuff, which I'm happy to report, nothing has changed.  At all.  Not even a little bit.  Consider this blog entry by a security optimist.  He's hopeful there's a more better way to solve the security resistance, so I posted the following comment, less some unnecessary info.

Users are focused on their job, nothing more.  Security must be either transparent or enforced by the bosses/masses.  Take this example...

You have a 3 year old that's obviously sick.  At first, the idea would be to allow the child to take something to make him/her better, willingly, but after say, 5 attempts, the forceful method is used and the child gets better.  The child learns as bad as this is, it's better to take it willingly because in the end, the outcome is favorable.  Where's the gap? 

Security from a users perspective never goes from "sick" to "better" based on their actions.  It never went from "better" to "sick" based on their actions either.  This goes for most managers and home users too.  It's the "all of a sudden" syndrome.  Even worse, it's really hard to get back to "better" when the "sick" level has been reached, especially when things like credit cards, SSNs, etc have been compromised.  Bad day, UpdateResume();

The insane amount of work comes by making it transparent and necessary.  It's really easy to flip a switch and say "its on"; it's hard to have someone come into a room then a camera and a finger print reader decides if they're allowed to see the room and then the lights come on and the door opens based on that decision.  It's even harder to determine "what is necessary" and for what people at what time.  There's no magic formula.

I believe that in small shops, this is easily obtainable thanks to sense of ownership.  Large companies, not so much.

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

Security

Sitefinity and security based generic content areas

by Jesse 10. July 2008 03:05

as I discovered, sitefinity doesn't support public side permissions out of the box.  It does on the admin side, just not the public facing side.  The particular project I'm on now requires this to a rather high complexity level.  Ok, time to bust out the goods.

I hit their developer manual and find something that looks promising for a couple reasons.  It uses similiar methods I tried thinking it was out of the box.  What I want to interject is this tiny piece of logic...

if (!Roles.IsUserInRole("SomeRole")
{
     // show some stuff!
}
else
{
     // show me nothing!
}

Nothing fancy, nothing over the top just straight and to the point.  That -should- give me what I need ...now, how to make it happen is another task.

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

Powered by BlogEngine.NET 1.4.5.0
Theme by Mads Kristensen

About the author

Like the description says, at my core, I'm a scientist and engineer.  I came from humble beginnings on a 486DX2 Packard Hell playing doom2 on IPX to in a small time retail shop and got into hardware (ISO layers FTW!) and it was all downhill from there.  I'm infinitely curious about almost everything and always wanting to know.

Some of the stuff I'm currently into/researching...

Sitefinity

Ninject

Subsonic

Java

Currently working on ...
i did the hundred 
and some extra stuff

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's, their brother nor their dog's view in anyway.  At all.  Ever.

© Copyright 2007-2008